forked from rossja/TinyNuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP.cpp
196 lines (182 loc) · 5.79 KB
/
HTTP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "HTTP.h"
BOOL HttpSubmitRequest(HttpRequestData &httpRequestData)
{
BOOL ret = FALSE;
WSADATA wsa;
SOCKET s;
char request[1024] = { 0 };
httpRequestData.outputBodySize = 0;
Funcs::pLstrcpyA(request, (httpRequestData.post ? Strs::postSpace : Strs::getSpace));
Funcs::pLstrcatA(request, httpRequestData.path);
Funcs::pLstrcatA(request, Strs::httpReq1);
Funcs::pLstrcatA(request, Strs::httpReq2);
Funcs::pLstrcatA(request, httpRequestData.host);
Funcs::pLstrcatA(request, Strs::httpReq3);
if(httpRequestData.post && httpRequestData.inputBody)
{
Funcs::pLstrcatA(request, Strs::httpReq4);
char sizeStr[10];
Funcs::pWsprintfA(sizeStr, Strs::sprintfIntEscape, httpRequestData.inputBodySize);
Funcs::pLstrcatA(request, sizeStr);
Funcs::pLstrcatA(request, Strs::winNewLine);
}
Funcs::pLstrcatA(request, Strs::winNewLine);
if(Funcs::pWSAStartup(MAKEWORD(2, 2), &wsa) != 0)
goto exit;
if((s = Funcs::pSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
goto exit;
hostent *he = Funcs::pGethostbyname(httpRequestData.host);
if(!he)
goto exit;
struct sockaddr_in addr;
Funcs::pMemcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length);
addr.sin_family = AF_INET;
addr.sin_port = Funcs::pHtons(httpRequestData.port);
if(Funcs::pConnect(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR)
goto exit;
if(Funcs::pSend(s, request, Funcs::pLstrlenA(request), 0) <= 0)
goto exit;
if(httpRequestData.inputBody)
{
if(Funcs::pSend(s, (char *) httpRequestData.inputBody, httpRequestData.inputBodySize, 0) <= 0)
goto exit;
}
char header[1024] = { 0 };
int contentLength = -1;
int lastPos = 0;
BOOL firstLine = TRUE;
BOOL transferChunked = FALSE;
for(int i = 0;; ++i)
{
if(i > sizeof(header) - 1)
goto exit;
if(Funcs::pRecv(s, header + i, 1, 0) <= 0)
goto exit;
if(i > 0 && header[i - 1] == '\r' && header[i] == '\n')
{
header[i - 1] = 0;
if(firstLine)
{
if(Funcs::pLstrcmpiA(header, Strs::httpReq5))
goto exit;
firstLine = FALSE;
}
else
{
char *field = header + lastPos + 2;
if(Funcs::pLstrlenA(field) == 0)
{
if(contentLength < 0 && !transferChunked)
goto exit;
break;
}
char *name;
char *value;
if((value = (char *) Funcs::pStrStrA(field, Strs::httpReq6)))
{
name = field;
name[value - field] = 0;
value += 2;
if(!Funcs::pLstrcmpiA(name, Strs::httpReq7))
{
char *endPtr;
contentLength = Funcs::pStrtol(value, &endPtr, 10);
if(endPtr == value)
goto exit;
if(value < 0)
goto exit;
}
else if(!Funcs::pLstrcmpiA(name, Strs::httpReq8))
{
if(!Funcs::pLstrcmpiA(value, Strs::httpReq9))
transferChunked = TRUE;
}
value += 2;
}
}
lastPos = i - 1;
}
}
if(transferChunked)
{
const int reallocSize = 16394;
char sizeStr[10] = { 0 };
int allocatedSize = reallocSize;
int read = 0;
httpRequestData.outputBody = (BYTE *) Alloc(reallocSize);
for(int i = 0;;)
{
if(i > sizeof(sizeStr) - 1)
goto exit;
if(Funcs::pRecv(s, sizeStr + i, 1, 0) <= 0)
goto exit;
if(i > 0 && sizeStr[i - 1] == '\r' && sizeStr[i] == '\n')
{
sizeStr[i - 1] = 0;
char *endPtr;
int size = Funcs::pStrtol(sizeStr, &endPtr, 16);
if(endPtr == sizeStr)
goto exit;
if(size < 0)
goto exit;
if(size == 0)
{
httpRequestData.outputBody[httpRequestData.outputBodySize] = 0;
break;
}
httpRequestData.outputBodySize += size;
if(allocatedSize < httpRequestData.outputBodySize + 1)
{
allocatedSize += httpRequestData.outputBodySize + reallocSize;
httpRequestData.outputBody = (BYTE *) ReAlloc(httpRequestData.outputBody, allocatedSize);
}
int chunkRead = 0;
do
{
int read2 = Funcs::pRecv(s, (char *) httpRequestData.outputBody + read + chunkRead, size - chunkRead, 0);
if(read2 <= 0)
goto exit;
chunkRead += read2;
} while(chunkRead != size);
if(Funcs::pRecv(s, sizeStr, 2, 0) <= 0)
goto exit;
read += size;
i = 0;
continue;
}
++i;
}
}
else
{
if(contentLength > 0)
{
httpRequestData.outputBody = (BYTE *) Alloc(contentLength + 1);
httpRequestData.outputBodySize = contentLength;
httpRequestData.outputBody[httpRequestData.outputBodySize] = 0;
int totalRead = 0;
do
{
int read = Funcs::pRecv(s, (char *) httpRequestData.outputBody + totalRead, contentLength - totalRead, 0);
if(read <= 0) goto exit;
totalRead += read;
}
while(totalRead != contentLength);
}
else
{
httpRequestData.outputBody = (BYTE *) Alloc(1);
httpRequestData.outputBody[0] = 0;
}
}
ret = TRUE;
exit:
if(!ret)
{
httpRequestData.outputBody = NULL;
Funcs::pFree(httpRequestData.outputBody);
}
Funcs::pClosesocket(s);
Funcs::pWSACleanup();
return ret;
}