123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #include <stdio.h>
- #include <string.h>
- #ifdef _WIN32
- #include <io.h>
- #include <winsock2.h>
- #define snprintf _snprintf
- #else
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #endif
- #include "minisoap.h"
- #include "miniupnpcstrings.h"
- #include <stdlib.h>
- #ifdef _WIN32
- #define PRINT_SOCKET_ERROR(x) fprintf(stderr, "Socket error: %s, %d\n", x, WSAGetLastError());
- #else
- #define PRINT_SOCKET_ERROR(x) perror(x)
- #endif
- static int
- httpWrite(SOCKET fd, const char * body, int bodysize,
- const char * headers, int headerssize)
- {
- int n = 0;
-
-
-
- char * p;
-
- p = malloc(headerssize+bodysize);
- if(!p)
- return -1;
- memcpy(p, headers, headerssize);
- memcpy(p+headerssize, body, bodysize);
-
- n = send(fd, p, headerssize+bodysize, 0);
- if(n<0) {
- PRINT_SOCKET_ERROR("send");
- }
-
-
- #if 0
- #ifdef _WIN32
- if(shutdown(fd, SD_SEND)<0) {
- #else
- if(shutdown(fd, SHUT_WR)<0) {
- #endif
- PRINT_SOCKET_ERROR("shutdown");
- }
- #endif
- free(p);
- return n;
- }
- int soapPostSubmit(SOCKET fd,
- const char * url,
- const char * host,
- unsigned short port,
- const char * action,
- const char * body,
- const char * httpversion)
- {
- int bodysize;
- char headerbuf[512];
- int headerssize;
- char portstr[8];
- bodysize = (int)strlen(body);
-
-
- portstr[0] = '\0';
- if(port != 80)
- snprintf(portstr, sizeof(portstr), ":%hu", port);
- headerssize = snprintf(headerbuf, sizeof(headerbuf),
- "POST %s HTTP/%s\r\n"
- "Host: %s%s\r\n"
- "User-Agent: " OS_STRING ", " UPNP_VERSION_STRING ", MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
- "Content-Length: %d\r\n"
- "Content-Type: text/xml\r\n"
- "SOAPAction: \"%s\"\r\n"
- "Connection: Close\r\n"
- "Cache-Control: no-cache\r\n"
- "Pragma: no-cache\r\n"
- "\r\n",
- url, httpversion, host, portstr, bodysize, action);
- if ((unsigned int)headerssize >= sizeof(headerbuf))
- return -1;
- #ifdef DEBUG
-
- printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
- url, httpversion, host, portstr);
- printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);
- printf("Headers :\n%s", headerbuf);
- printf("Body :\n%s\n", body);
- #endif
- return httpWrite(fd, body, bodysize, headerbuf, headerssize);
- }
|