/thirdparty/libportfwd/third-party/miniupnpc-1.6/minisoap.c

http://github.com/tomahawk-player/tomahawk · C · 121 lines · 83 code · 6 blank · 32 comment · 6 complexity · e0327592bc4c53bef7be524573477e6c MD5 · raw file

  1. /* $Id: minisoap.c,v 1.21 2011/03/22 19:15:35 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2005-2009 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution.
  7. *
  8. * Minimal SOAP implementation for UPnP protocol.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #ifdef WIN32
  13. #include <io.h>
  14. #include <winsock2.h>
  15. #define snprintf _snprintf
  16. #else
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #endif
  21. #include "minisoap.h"
  22. #include "miniupnpcstrings.h"
  23. /* only for malloc */
  24. #include <stdlib.h>
  25. #ifdef WIN32
  26. #define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError());
  27. #else
  28. #define PRINT_SOCKET_ERROR(x) perror(x)
  29. #endif
  30. /* httpWrite sends the headers and the body to the socket
  31. * and returns the number of bytes sent */
  32. static int
  33. httpWrite(int fd, const char * body, int bodysize,
  34. const char * headers, int headerssize)
  35. {
  36. int n = 0;
  37. /*n = write(fd, headers, headerssize);*/
  38. /*if(bodysize>0)
  39. n += write(fd, body, bodysize);*/
  40. /* Note : my old linksys router only took into account
  41. * soap request that are sent into only one packet */
  42. char * p;
  43. /* TODO: AVOID MALLOC */
  44. p = malloc(headerssize+bodysize);
  45. if(!p)
  46. return 0;
  47. memcpy(p, headers, headerssize);
  48. memcpy(p+headerssize, body, bodysize);
  49. /*n = write(fd, p, headerssize+bodysize);*/
  50. n = send(fd, p, headerssize+bodysize, 0);
  51. if(n<0) {
  52. PRINT_SOCKET_ERROR("send");
  53. }
  54. /* disable send on the socket */
  55. /* draytek routers dont seems to like that... */
  56. #if 0
  57. #ifdef WIN32
  58. if(shutdown(fd, SD_SEND)<0) {
  59. #else
  60. if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/
  61. #endif
  62. PRINT_SOCKET_ERROR("shutdown");
  63. }
  64. #endif
  65. free(p);
  66. return n;
  67. }
  68. /* self explanatory */
  69. int soapPostSubmit(int fd,
  70. const char * url,
  71. const char * host,
  72. unsigned short port,
  73. const char * action,
  74. const char * body,
  75. const char * httpversion)
  76. {
  77. int bodysize;
  78. char headerbuf[512];
  79. int headerssize;
  80. char portstr[8];
  81. bodysize = (int)strlen(body);
  82. /* We are not using keep-alive HTTP connections.
  83. * HTTP/1.1 needs the header Connection: close to do that.
  84. * This is the default with HTTP/1.0
  85. * Using HTTP/1.1 means we need to support chunked transfer-encoding :
  86. * When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked
  87. * transfer encoding. */
  88. /* Connection: Close is normally there only in HTTP/1.1 but who knows */
  89. portstr[0] = '\0';
  90. if(port != 80)
  91. snprintf(portstr, sizeof(portstr), ":%hu", port);
  92. headerssize = snprintf(headerbuf, sizeof(headerbuf),
  93. "POST %s HTTP/%s\r\n"
  94. "Host: %s%s\r\n"
  95. "User-Agent: " OS_STRING ", UPnP/1.0, MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
  96. "Content-Length: %d\r\n"
  97. "Content-Type: text/xml\r\n"
  98. "SOAPAction: \"%s\"\r\n"
  99. "Connection: Close\r\n"
  100. "Cache-Control: no-cache\r\n" /* ??? */
  101. "Pragma: no-cache\r\n"
  102. "\r\n",
  103. url, httpversion, host, portstr, bodysize, action);
  104. #ifdef DEBUG
  105. /*printf("SOAP request : headersize=%d bodysize=%d\n",
  106. headerssize, bodysize);
  107. */
  108. printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
  109. url, httpversion, host, portstr);
  110. printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);
  111. printf("Headers :\n%s", headerbuf);
  112. printf("Body :\n%s\n", body);
  113. #endif
  114. return httpWrite(fd, body, bodysize, headerbuf, headerssize);
  115. }