source: trunk/third-party/miniupnp/minisoap.c @ 5094

Last change on this file since 5094 was 5094, checked in by charles, 14 years ago

update to miniupnpc-1.0

File size: 3.0 KB
RevLine 
[5094]1/* $Id: minisoap.c,v 1.15 2008/02/17 17:57:07 nanard Exp $ */
[3731]2/* Project : miniupnp
3 * Author : Thomas Bernard
4 * Copyright (c) 2005 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
23/* only for malloc */
24#include <stdlib.h>
25
[5087]26#ifdef WIN32
27#define PRINT_SOCKET_ERROR(x)    printf("Socket error: %s, %d\n", x, WSAGetLastError());
28#else
29#define PRINT_SOCKET_ERROR(x) perror(x)
30#endif
31
[3731]32/* httpWrite sends the headers and the body to the socket
33 * and returns the number of bytes sent */
34static int
35httpWrite(int fd, const char * body, int bodysize,
36          const char * headers, int headerssize)
37{
38        int n = 0;
39        /*n = write(fd, headers, headerssize);*/
40        /*if(bodysize>0)
41                n += write(fd, body, bodysize);*/
42        /* Note : my old linksys router only took into account
43         * soap request that are sent into only one packet */
44        char * p;
[5087]45        /* TODO: AVOID MALLOC */
[3731]46        p = malloc(headerssize+bodysize);
[5094]47        if(!p)
48          return 0;
[3731]49        memcpy(p, headers, headerssize);
50        memcpy(p+headerssize, body, bodysize);
51        /*n = write(fd, p, headerssize+bodysize);*/
52        n = send(fd, p, headerssize+bodysize, 0);
[5087]53        if(n<0) {
54          PRINT_SOCKET_ERROR("send");
55        }
56        /* disable send on the socket */
57        /* draytek routers dont seems to like that... */
58#if 0
[3731]59#ifdef WIN32
[5087]60        if(shutdown(fd, SD_SEND)<0) {
[3731]61#else
[5087]62        if(shutdown(fd, SHUT_WR)<0)     { /*SD_SEND*/
[3731]63#endif
[5087]64                PRINT_SOCKET_ERROR("shutdown");
65        }
66#endif
[3731]67        free(p);
68        return n;
69}
70
71/* self explanatory  */
72int soapPostSubmit(int fd,
73                   const char * url,
74                                   const char * host,
75                                   unsigned short port,
76                                   const char * action,
77                                   const char * body)
78{
79        int bodysize;
[5094]80        char headerbuf[512];
[3731]81        int headerssize;
[5094]82        char portstr[8];
[3731]83        bodysize = (int)strlen(body);
[4251]84        /* We are not using keep-alive HTTP connections.
85         * HTTP/1.1 needs the header Connection: close to do that.
86         * This is the default with HTTP/1.0 */
[5087]87    /* Connection: Close is normally there only in HTTP/1.1 but who knows */
[5094]88        portstr[0] = '\0';
89        if(port != 80)
90                snprintf(portstr, sizeof(portstr), ":%hu", port);
[3731]91        headerssize = snprintf(headerbuf, sizeof(headerbuf),
[5094]92                       "POST %s HTTP/1.1\r\n"
93/*                       "POST %s HTTP/1.0\r\n"*/
94                           "Host: %s%s\r\n"
[4251]95                                           "User-Agent: POSIX, UPnP/1.0, miniUPnPc/1.0\r\n"
96                           "Content-Length: %d\r\n"
[3731]97                                           "Content-Type: text/xml\r\n"
98                                           "SOAPAction: \"%s\"\r\n"
[5087]99                                           "Connection: Close\r\n"
[5094]100                                           "Cache-Control: no-cache\r\n"        /* ??? */
101                                           "Pragma: no-cache\r\n"
[3731]102                                           "\r\n",
[5094]103                                           url, host, portstr, bodysize, action);
104#ifdef DEBUG
105        printf("SOAP request : headersize=%d bodysize=%d\n",
106               headerssize, bodysize);
107        /*printf("%s", headerbuf);*/
108#endif
[3731]109        return httpWrite(fd, body, bodysize, headerbuf, headerssize);
110}
111
112
Note: See TracBrowser for help on using the repository browser.