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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
Copyright (c) 2009
Lars-Dominik Braun <PromyLOPh@lavabit.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "config.h"
#include "waitress.h"
typedef struct {
char *buf;
char *pos;
size_t max;
} WaitressFetchBufCbBuffer_t;
inline void WaitressInit (WaitressHandle_t *waith) {
memset (waith, 0, sizeof (*waith));
}
inline void WaitressFree (WaitressHandle_t *waith) {
memset (waith, 0, sizeof (*waith));
}
/* Proxy set up?
* @param Waitress handle
* @return true|false
*/
inline char WaitressProxyEnabled (const WaitressHandle_t *waith) {
return *waith->proxyHost != '\0' && *waith->proxyPort != '\0';
}
/* Set http proxy
* @param waitress handle
* @param host
* @param port
*/
inline void WaitressSetProxy (WaitressHandle_t *waith, const char *host,
const char *port) {
strncpy (waith->proxyHost, host, sizeof (waith->proxyHost)-1);
strncpy (waith->proxyPort, port, sizeof (waith->proxyPort)-1);
}
/* urlencode post-data
* @param encode this
* @return malloc'ed encoded string, don't forget to free it
*/
char *WaitressUrlEncode (const char *in) {
/* worst case: encode all characters */
size_t inLen = strlen (in);
char *out = calloc (inLen * 3 + 1, sizeof (*in));
const char *inPos = in;
char *outPos = out;
while (inPos - in < inLen) {
if (!isalnum (*inPos) && *inPos != '_' && *inPos != '-' && *inPos != '.') {
*outPos++ = '%';
snprintf (outPos, 3, "%02x", *inPos);
outPos += 2;
} else {
/* copy character */
*outPos++ = *inPos;
}
++inPos;
}
return out;
}
/* Split http url into host, port and path
* @param url
* @param return buffer: host
* @param host buffer size
* @param return buffer: port, defaults to 80
* @param port buffer size
* @param return buffer: path
* @param path buffer size
* @param 1 = ok, 0 = not a http url; if your buffers are too small horrible
* things will happen... But 1 is returned anyway.
*/
char WaitressSplitUrl (const char *url, char *retHost, size_t retHostSize,
char *retPort, size_t retPortSize, char *retPath, size_t retPathSize) {
size_t urlSize = strlen (url);
const char *urlPos = url, *lastPos;
if (urlSize > sizeof ("http://")-1 &&
memcmp (url, "http://", sizeof ("http://")-1) == 0) {
memset (retHost, 0, retHostSize);
memset (retPort, 0, retPortSize);
strncpy (retPort, "80", retPortSize-1);
memset (retPath, 0, retPathSize);
urlPos += sizeof ("http://")-1;
lastPos = urlPos;
/* find host */
while (*urlPos != ':' && *urlPos != '/' && urlPos - lastPos < retHostSize-1) {
*retHost++ = *urlPos++;
}
lastPos = urlPos;
/* port, if available */
if (*urlPos == ':') {
/* skip : */
++urlPos;
++lastPos;
while (*urlPos != '/' && urlPos - lastPos < retPortSize-1) {
*retPort++ = *urlPos++;
}
}
lastPos = urlPos;
/* path */
while (*urlPos != '\0' && *urlPos != '#' && urlPos - lastPos < retPathSize-1) {
*retPath++ = *urlPos++;
}
} else {
return 0;
}
return 1;
}
inline char WaitressSetUrl (WaitressHandle_t *waith, const char *url) {
return WaitressSplitUrl (url, waith->host, sizeof (waith->host),
waith->port, sizeof (waith->port), waith->path, sizeof (waith->path));
}
inline void WaitressSetHPP (WaitressHandle_t *waith, const char *host,
const char *port, const char *path) {
strncpy (waith->host, host, sizeof (waith->host)-1);
strncpy (waith->port, port, sizeof (waith->port)-1);
strncpy (waith->path, path, sizeof (waith->path)-1);
}
/* Callback for WaitressFetchBuf, appends received data to NULL-terminated buffer
* @param received data
* @param data size
* @param buffer structure
*/
char WaitressFetchBufCb (void *recvData, size_t recvDataSize, void *extraData) {
char *recvBytes = recvData;
WaitressFetchBufCbBuffer_t *buffer = extraData;
if (buffer->pos - buffer->buf + recvDataSize < buffer->max) {
memcpy (buffer->pos, recvBytes, recvDataSize);
buffer->pos += recvDataSize;
*buffer->pos = '\0';
} else {
printf (PACKAGE ": Buffer overflow!\n");
return 0;
}
return 1;
}
/* Fetch string. Beware! This overwrites your waith->data pointer
* @param waitress handle
* @param Connect to host
* @param Port or service
* @param Resource path
* @param result buffer
* @param result buffer size
*/
WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char *buf,
size_t bufSize) {
WaitressFetchBufCbBuffer_t buffer;
buffer.buf = buffer.pos = buf;
buffer.max = bufSize;
waith->data = &buffer;
waith->callback = WaitressFetchBufCb;
return WaitressFetchCall (waith);
}
#define CLOSE_RET(ret) close (sockfd); return ret;
/* Receive data from host and call *callback ()
* @param waitress handle
* @param host
* @param port
* @param absolute path
* @param callback function
* @return WaitressReturn_t
*/
WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) {
struct addrinfo hints, *res;
int sockfd;
char recvBuf[WAITRESS_RECV_BUFFER];
char statusBuf[3];
char writeBuf[2*1024];
ssize_t recvSize = 0;
WaitressReturn_t wRet = WAITRESS_RET_OK;
/* initialize */
waith->contentLength = 0;
waith->contentReceived = 0;
memset (&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* Use proxy? */
if (WaitressProxyEnabled (waith)) {
if (getaddrinfo (waith->proxyHost, waith->proxyPort, &hints, &res) != 0) {
return WAITRESS_RET_GETADDR_ERR;
}
} else {
if (getaddrinfo (waith->host, waith->port, &hints, &res) != 0) {
return WAITRESS_RET_GETADDR_ERR;
}
}
if ((sockfd = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
freeaddrinfo (res);
return WAITRESS_RET_SOCK_ERR;
}
if (connect (sockfd, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo (res);
return WAITRESS_RET_CONNECT_REFUSED;
}
freeaddrinfo (res);
/* send request */
if (WaitressProxyEnabled (waith)) {
snprintf (writeBuf, sizeof (writeBuf),
"%s http://%s:%s%s HTTP/1.1\r\nProxy-Connection: close\r\n",
(waith->method == WAITRESS_METHOD_GET ? "GET" : "POST"),
waith->host, waith->port, waith->path);
} else {
snprintf (writeBuf, sizeof (writeBuf),
"%s %s HTTP/1.1\r\nConnection: close\r\n",
(waith->method == WAITRESS_METHOD_GET ? "GET" : "POST"),
waith->path);
}
write (sockfd, writeBuf, strlen (writeBuf));
snprintf (writeBuf, sizeof (writeBuf),
"Host: %s\r\nUser-Agent: " PACKAGE "\r\n", waith->host);
write (sockfd, writeBuf, strlen (writeBuf));
if (waith->method == WAITRESS_METHOD_POST && waith->postData != NULL) {
snprintf (writeBuf, sizeof (writeBuf), "Content-Length: %u\r\n",
strlen (waith->postData));
write (sockfd, writeBuf, strlen (writeBuf));
}
if (waith->extraHeaders != NULL) {
write (sockfd, waith->extraHeaders, strlen (waith->extraHeaders));
}
write (sockfd, "\r\n", 2);
if (waith->method == WAITRESS_METHOD_POST && waith->postData != NULL) {
write (sockfd, waith->postData, strlen (waith->postData));
}
/* throw "HTTP/1.1 " (length 9) away... */
read (sockfd, recvBuf, 9);
/* parse status code */
read (sockfd, statusBuf, sizeof (statusBuf));
switch (statusBuf[0]) {
case '2':
if (statusBuf[1] == '0' &&
(statusBuf[2] == '0' || statusBuf[2] == '6')) {
/* 200 OK/206 Partial Content */
} else {
CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN);
}
break;
case '4':
/* 40X? */
switch (statusBuf[1]) {
case '0':
switch (statusBuf[2]) {
case '3':
/* 403 Forbidden */
CLOSE_RET (WAITRESS_RET_FORBIDDEN);
break;
case '4':
/* 404 Not found */
CLOSE_RET (WAITRESS_RET_NOTFOUND);
break;
default:
CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN);
break;
}
break;
default:
CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN);
break;
}
break;
default:
CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN);
break;
}
/* read and parse header, max sizeof(recvBuf) */
char *bufPos = recvBuf, *hdrMark = NULL, *hdrBegin = recvBuf;
recvSize = read (sockfd, recvBuf, sizeof (recvBuf));
while (bufPos - recvBuf < recvSize) {
/* mark header name end */
if (*bufPos == ':' && hdrMark == NULL) {
hdrMark = bufPos;
} else if (*bufPos == '\n') {
/* parse header */
if (hdrMark != NULL && (bufPos - hdrBegin > 12) &&
memcmp ("Content-Length", hdrBegin, 12) == 0) {
/* unsigned integer max: 4,294,967,295 = 10 digits */
char tmpSizeBuf[11];
memset (tmpSizeBuf, 0, sizeof (tmpSizeBuf));
/* skip ':' => +1; skip '\r' suffix => -1 */
if ((bufPos-1) - (hdrMark+1) > sizeof (tmpSizeBuf)-1) {
CLOSE_RET (WAITRESS_RET_HDR_OVERFLOW);
}
memcpy (tmpSizeBuf, hdrMark+1, (bufPos-1) - (hdrMark+1));
waith->contentLength = atol (tmpSizeBuf);
}
/* end header, containts \r\n == empty line */
if (bufPos - hdrBegin < 2 && hdrMark == NULL) {
++bufPos;
/* push remaining bytes to callback */
if (bufPos - recvBuf < recvSize) {
waith->contentReceived += recvSize - (bufPos - recvBuf);
if (!waith->callback (bufPos, recvSize - (bufPos - recvBuf),
waith->data)) {
CLOSE_RET (WAITRESS_RET_CB_ABORT);
}
}
break;
}
hdrBegin = bufPos+1;
hdrMark = NULL;
}
++bufPos;
}
/* receive content */
do {
recvSize = read (sockfd, recvBuf, sizeof (recvBuf));
if (recvSize > 0) {
waith->contentReceived += recvSize;
if (!waith->callback (recvBuf, recvSize, waith->data)) {
wRet = WAITRESS_RET_CB_ABORT;
break;
}
}
} while (recvSize > 0);
close (sockfd);
if (wRet == WAITRESS_RET_OK && waith->contentReceived < waith->contentLength) {
return WAITRESS_RET_PARTIAL_FILE;
}
return wRet;
}
#undef CLOSE_RET
|