1 | /* $Id: minisoap.c,v 1.12 2007/12/13 17:09:03 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 | /* httpWrite sends the headers and the body to the socket |
---|
27 | * and returns the number of bytes sent */ |
---|
28 | static int |
---|
29 | httpWrite(int fd, const char * body, int bodysize, |
---|
30 | const char * headers, int headerssize) |
---|
31 | { |
---|
32 | int n = 0; |
---|
33 | /*n = write(fd, headers, headerssize);*/ |
---|
34 | /*if(bodysize>0) |
---|
35 | n += write(fd, body, bodysize);*/ |
---|
36 | /* Note : my old linksys router only took into account |
---|
37 | * soap request that are sent into only one packet */ |
---|
38 | char * p; |
---|
39 | p = malloc(headerssize+bodysize); |
---|
40 | memcpy(p, headers, headerssize); |
---|
41 | memcpy(p+headerssize, body, bodysize); |
---|
42 | /*n = write(fd, p, headerssize+bodysize);*/ |
---|
43 | n = send(fd, p, headerssize+bodysize, 0); |
---|
44 | #ifdef WIN32 |
---|
45 | shutdown(fd, SD_SEND); |
---|
46 | #else |
---|
47 | shutdown(fd, SHUT_WR); /*SD_SEND*/ |
---|
48 | #endif |
---|
49 | free(p); |
---|
50 | return n; |
---|
51 | } |
---|
52 | |
---|
53 | /* self explanatory */ |
---|
54 | int soapPostSubmit(int fd, |
---|
55 | const char * url, |
---|
56 | const char * host, |
---|
57 | unsigned short port, |
---|
58 | const char * action, |
---|
59 | const char * body) |
---|
60 | { |
---|
61 | int bodysize; |
---|
62 | char headerbuf[1024]; |
---|
63 | int headerssize; |
---|
64 | bodysize = (int)strlen(body); |
---|
65 | /* We are not using keep-alive HTTP connections. |
---|
66 | * HTTP/1.1 needs the header Connection: close to do that. |
---|
67 | * This is the default with HTTP/1.0 */ |
---|
68 | headerssize = snprintf(headerbuf, sizeof(headerbuf), |
---|
69 | /* "POST %s HTTP/1.1\r\n"*/ |
---|
70 | "POST %s HTTP/1.0\r\n" |
---|
71 | "Host: %s:%d\r\n" |
---|
72 | "User-Agent: POSIX, UPnP/1.0, miniUPnPc/1.0\r\n" |
---|
73 | "Content-Length: %d\r\n" |
---|
74 | "Content-Type: text/xml\r\n" |
---|
75 | "SOAPAction: \"%s\"\r\n" |
---|
76 | /* "Connection: Close\r\n" */ |
---|
77 | "\r\n", |
---|
78 | url, host, port, bodysize, action); |
---|
79 | return httpWrite(fd, body, bodysize, headerbuf, headerssize); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|