Path: blob/master/thirdparty/miniupnpc/src/minisoap.c
9904 views
/* $Id: minisoap.c,v 1.35 2025/04/27 21:13:45 nanard Exp $ */1/* vim: tabstop=4 shiftwidth=4 noexpandtab2* Project : miniupnp3* Author : Thomas Bernard4* Copyright (c) 2005-2025 Thomas Bernard5* This software is subject to the conditions detailed in the6* 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 _WIN3213#define WIN32_LEAN_AND_MEAN14#include <io.h>15#include <winsock2.h>16#include "win32_snprintf.h"17#else18#include <unistd.h>19#include <sys/types.h>20#include <sys/socket.h>21#endif22#include "minisoap.h"23#include "miniupnpcstrings.h"2425/* only for malloc */26#include <stdlib.h>2728/* httpWrite sends the headers and the body to the socket29* and returns the number of bytes sent */30static int31httpWrite(SOCKET fd, const char * body, int bodysize,32const char * headers, int headerssize)33{34int n = 0;35/*n = write(fd, headers, headerssize);*/36/*if(bodysize>0)37n += write(fd, body, bodysize);*/38/* Note : my old linksys router only took into account39* soap request that are sent into only one packet */40char * p;41/* TODO: AVOID MALLOC, we could use writev() for that */42p = malloc(headerssize+bodysize);43if(!p)44return -1;45memcpy(p, headers, headerssize);46memcpy(p+headerssize, body, bodysize);47/*n = write(fd, p, headerssize+bodysize);*/48n = send(fd, p, headerssize+bodysize, 0);49if(n<0) {50PRINT_SOCKET_ERROR("send");51}52/* disable send on the socket */53/* draytek routers don't seem to like that... */54#if 055#ifdef _WIN3256if(shutdown(fd, SD_SEND)<0) {57#else58if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/59#endif60PRINT_SOCKET_ERROR("shutdown");61}62#endif63free(p);64return n;65}6667/* self explanatory */68int soapPostSubmit(SOCKET fd,69const char * url,70const char * host,71unsigned short port,72const char * action,73const char * body,74const char * httpversion)75{76char headerbuf[512];77int headerssize;78char portstr[8];79int bodysize = (int)strlen(body);80/* We are not using keep-alive HTTP connections.81* HTTP/1.1 needs the header Connection: close to do that.82* This is the default with HTTP/1.083* Using HTTP/1.1 means we need to support chunked transfer-encoding :84* When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked85* transfer encoding. */86/* Connection: close is normally there only in HTTP/1.1 but who knows */87portstr[0] = '\0';88if(port != 80)89snprintf(portstr, sizeof(portstr), ":%hu", port);90headerssize = snprintf(headerbuf, sizeof(headerbuf),91"POST %s HTTP/%s\r\n"92"Host: %s%s\r\n"93"User-Agent: " OS_STRING " " UPNP_VERSION_STRING " MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"94"Content-Length: %d\r\n"95#if (UPNP_VERSION_MAJOR == 1) && (UPNP_VERSION_MINOR == 0)96"Content-Type: text/xml\r\n"97#else98"Content-Type: text/xml; charset=\"utf-8\"\r\n"99#endif100"SOAPAction: \"%s\"\r\n"101"Connection: close\r\n"102"\r\n",103url, httpversion, host, portstr, bodysize, action);104if ((unsigned int)headerssize >= sizeof(headerbuf))105return -1;106#ifdef DEBUG107/*printf("SOAP request : headersize=%d bodysize=%d\n",108headerssize, bodysize);109*/110printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",111url, httpversion, host, portstr);112printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);113printf("Headers :\n%s", headerbuf);114printf("Body :\n%s\n", body);115#endif116return httpWrite(fd, body, bodysize, headerbuf, headerssize);117}118119120121122