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

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

update to miniupnp-20080217

File size: 2.7 KB
Line 
1/* $Id: minisoap.c,v 1.14 2008/02/16 23:46:11 nanard Exp $ */
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
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
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;
45        /* TODO: AVOID MALLOC */
46        p = malloc(headerssize+bodysize);
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
69/* self explanatory  */
70int soapPostSubmit(int fd,
71                   const char * url,
72                                   const char * host,
73                                   unsigned short port,
74                                   const char * action,
75                                   const char * body)
76{
77        int bodysize;
78        char headerbuf[1024];
79        int headerssize;
80        bodysize = (int)strlen(body);
81        /* We are not using keep-alive HTTP connections.
82         * HTTP/1.1 needs the header Connection: close to do that.
83         * This is the default with HTTP/1.0 */
84    /* Connection: Close is normally there only in HTTP/1.1 but who knows */
85        headerssize = snprintf(headerbuf, sizeof(headerbuf),
86/*                       "POST %s HTTP/1.1\r\n"*/
87                       "POST %s HTTP/1.0\r\n"
88                           "Host: %s:%d\r\n"
89                                           "User-Agent: POSIX, UPnP/1.0, miniUPnPc/1.0\r\n"
90                           "Content-Length: %d\r\n"
91                                           "Content-Type: text/xml\r\n"
92                                           "SOAPAction: \"%s\"\r\n"
93                                           "Connection: Close\r\n"
94                                           "\r\n",
95                                           url, host, port, bodysize, action);
96        return httpWrite(fd, body, bodysize, headerbuf, headerssize);
97}
98
99
Note: See TracBrowser for help on using the repository browser.