Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/miniupnpc/src/minisoap.c
9904 views
1
/* $Id: minisoap.c,v 1.35 2025/04/27 21:13:45 nanard Exp $ */
2
/* vim: tabstop=4 shiftwidth=4 noexpandtab
3
* Project : miniupnp
4
* Author : Thomas Bernard
5
* Copyright (c) 2005-2025 Thomas Bernard
6
* This software is subject to the conditions detailed in the
7
* LICENCE file provided in this distribution.
8
*
9
* Minimal SOAP implementation for UPnP protocol.
10
*/
11
#include <stdio.h>
12
#include <string.h>
13
#ifdef _WIN32
14
#define WIN32_LEAN_AND_MEAN
15
#include <io.h>
16
#include <winsock2.h>
17
#include "win32_snprintf.h"
18
#else
19
#include <unistd.h>
20
#include <sys/types.h>
21
#include <sys/socket.h>
22
#endif
23
#include "minisoap.h"
24
#include "miniupnpcstrings.h"
25
26
/* only for malloc */
27
#include <stdlib.h>
28
29
/* httpWrite sends the headers and the body to the socket
30
* and returns the number of bytes sent */
31
static int
32
httpWrite(SOCKET fd, const char * body, int bodysize,
33
const char * headers, int headerssize)
34
{
35
int n = 0;
36
/*n = write(fd, headers, headerssize);*/
37
/*if(bodysize>0)
38
n += write(fd, body, bodysize);*/
39
/* Note : my old linksys router only took into account
40
* soap request that are sent into only one packet */
41
char * p;
42
/* TODO: AVOID MALLOC, we could use writev() for that */
43
p = malloc(headerssize+bodysize);
44
if(!p)
45
return -1;
46
memcpy(p, headers, headerssize);
47
memcpy(p+headerssize, body, bodysize);
48
/*n = write(fd, p, headerssize+bodysize);*/
49
n = send(fd, p, headerssize+bodysize, 0);
50
if(n<0) {
51
PRINT_SOCKET_ERROR("send");
52
}
53
/* disable send on the socket */
54
/* draytek routers don't seem to like that... */
55
#if 0
56
#ifdef _WIN32
57
if(shutdown(fd, SD_SEND)<0) {
58
#else
59
if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/
60
#endif
61
PRINT_SOCKET_ERROR("shutdown");
62
}
63
#endif
64
free(p);
65
return n;
66
}
67
68
/* self explanatory */
69
int soapPostSubmit(SOCKET 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
char headerbuf[512];
78
int headerssize;
79
char portstr[8];
80
int 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
* Using HTTP/1.1 means we need to support chunked transfer-encoding :
85
* When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked
86
* transfer encoding. */
87
/* Connection: close is normally there only in HTTP/1.1 but who knows */
88
portstr[0] = '\0';
89
if(port != 80)
90
snprintf(portstr, sizeof(portstr), ":%hu", port);
91
headerssize = snprintf(headerbuf, sizeof(headerbuf),
92
"POST %s HTTP/%s\r\n"
93
"Host: %s%s\r\n"
94
"User-Agent: " OS_STRING " " UPNP_VERSION_STRING " MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
95
"Content-Length: %d\r\n"
96
#if (UPNP_VERSION_MAJOR == 1) && (UPNP_VERSION_MINOR == 0)
97
"Content-Type: text/xml\r\n"
98
#else
99
"Content-Type: text/xml; charset=\"utf-8\"\r\n"
100
#endif
101
"SOAPAction: \"%s\"\r\n"
102
"Connection: close\r\n"
103
"\r\n",
104
url, httpversion, host, portstr, bodysize, action);
105
if ((unsigned int)headerssize >= sizeof(headerbuf))
106
return -1;
107
#ifdef DEBUG
108
/*printf("SOAP request : headersize=%d bodysize=%d\n",
109
headerssize, bodysize);
110
*/
111
printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
112
url, httpversion, host, portstr);
113
printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);
114
printf("Headers :\n%s", headerbuf);
115
printf("Body :\n%s\n", body);
116
#endif
117
return httpWrite(fd, body, bodysize, headerbuf, headerssize);
118
}
119
120
121
122