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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457 | /**
* @file libhttp.c
* @brief HTTP parsing and building library
*
* @author François Cerbelle (Fanfan), francois@cerbelle.net
*
* @internal
* Created: 15/11/2024
* Revision: none
* Last modified: 2024-11-23 15:52
* Compiler: gcc
* Organization: Cerbelle.net
* Copyright: Copyright (c) 2024, François Cerbelle
*
* This source code is released for free distribution under the terms of the
* GNU General Public License as published by the Free Software Foundation.
*/
#include "libhttp.h"
#include "sclist.h"
#include "base64.h" /* Base64 encoder and decoder */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
typedef struct HTTPHeader_s {
sclistrecord_t* self;
char* name;
char* value;
} HTTPHeader_t;
HTTPHeader_t* HTTPHeader_setname(HTTPHeader_t* header, const char* name) {<--- The function 'HTTPHeader_setname' is never used.
char* newname;
assert(header);
assert(header->name);
assert(header->value);
if (NULL==(newname=realloc(header->name,strlen(name)+1))) {
perror("HTTPHeader_setname realloc");
return NULL;
}
header->name = newname;
strcpy(header->name,name);
return header;
}
char* HTTPHeader_getname(HTTPHeader_t* header) {
assert(header);
assert(header->name);
assert(header->value);
return header->name;
}
HTTPHeader_t* HTTPHeader_setvalue(HTTPHeader_t* header, const char* value) {<--- The function 'HTTPHeader_setvalue' is never used.
char* newvalue;
assert(header);
assert(header->name);
assert(header->value);
if (NULL==(newvalue=realloc(header->value,strlen(value)+1))) {
perror("HTTPHeader_setvalue realloc");
return NULL;
}
header->value = newvalue;
strcpy(header->value,value);
return header;
}
char* HTTPHeader_getvalue(HTTPHeader_t* header) {
assert(header);
assert(header->name);
assert(header->value);
return header->value;
}
static HTTPHeader_t* HTTPHeader_new(const char* name, const char* value) {
HTTPHeader_t* header;
assert(name);
assert(value);
if (NULL==(header=malloc(sizeof(struct HTTPHeader_s)))) {
perror("HTTPHeader_new");
return NULL;
}
if (NULL==(header->name=strdup(name))) {
perror("HTTPHeader_new name");
free(header);
return NULL;
}
if (NULL==(header->value=strdup(value))) {
perror("HTTPHeader_new value");
free(header->name);
free(header);
return NULL;
}
header->self = NULL;
return header;
}
static void HTTPHeader_del(HTTPHeader_t* header) {
assert(header);
assert(header->name);
assert(header->value);
free(header->name);
free(header->value);
free(header);
}
typedef struct HTTP_s {
sclist_t* headers;
char* body;
} HTTP_t;
HTTP_t* HTTP_setbody(HTTP_t* http, const char* body) {
char* newbody;
assert(http);
assert(http->headers);
assert(http->body);
assert(body);
if (NULL==(newbody=realloc(http->body,strlen(body)+1))) {
perror("HTTP_setbody realloc");
return NULL;
}
http->body = newbody;
strcpy(http->body, body);
return http;
}
char* HTTP_getbody(HTTP_t* http) {
assert(http);
assert(http->headers);
assert(http->body);
return http->body;
}
HTTPHeader_t* HTTP_addheader(HTTP_t* http, const char* name, const char* value) {
HTTPHeader_t* header;
assert(http);
assert(http->headers);
assert(http->body);
assert(name);
assert(value);
if (NULL==(header = HTTPHeader_new(name,value))) {
perror("HTTP_addheader HTTPHeader_new");
return NULL;
}
if (NULL==(header->self=sclist_addrecord(http->headers,header))) {
perror("HTTP_addheader");
HTTPHeader_del(header);
return NULL;
}
return header;
}
HTTPHeader_t* HTTP_addbasicauth(HTTP_t* http, const char* login, const char* pass) {<--- The function 'HTTP_addbasicauth' is never used.
char* auth_encoded;
char* auth;
HTTPHeader_t* header;
assert(http);
assert(http->headers);
assert(http->body);
assert(login);
assert(pass);
if (NULL==(auth=malloc(strlen(login)+1+strlen(pass)+1))) {
perror("HTTP_addbasicauth credentials");
return NULL;
}
/* strcpy/strcat expected to be faster than sprintf */
strcpy(auth,login);
strcat(auth,":");
strcat(auth,pass);
auth_encoded=base64_encode(auth);
free(auth);
if (NULL==(auth=malloc(strlen("Basic ")+strlen(auth_encoded)+1))) {
perror("HTTP_addbasicauth value");
free(auth_encoded);
return NULL;
}
strcpy(auth,"Basic ");
strcat(auth, auth_encoded);
free(auth_encoded);
if (NULL==(header=HTTP_addheader(http,"Authorization",auth))) {
perror("HTTP_addbasicauth addheader");
free(auth);
return NULL;
}
free(auth);
return header;
}
HTTPHeader_t* HTTP_firstheader(const HTTP_t* http) {<--- The function 'HTTP_firstheader' is never used.
sclistrecord_t* headerlst_entry;
assert(http);
assert(http->headers);
assert(http->body);
if (NULL==(headerlst_entry=sclist_firstrecord(http->headers)))
return NULL;
return sclist_getvalue(headerlst_entry);
}
HTTPHeader_t* HTTP_nextheader(const HTTPHeader_t* header) {<--- The function 'HTTP_nextheader' is never used.
sclistrecord_t* headerlst_entry;
assert(header);
assert(header->self);
assert(header->name);
assert(header->value);
headerlst_entry = header->self;
/* EOL reached */
if (NULL==(headerlst_entry = sclist_nextrecord(headerlst_entry)))
return NULL;
else
return sclist_getvalue(headerlst_entry);
}
HTTPHeader_t* HTTP_findheader(const HTTPHeader_t* start, const char* name) {<--- The function 'HTTP_findheader' is never used.
sclistrecord_t* headerlst_entry;
assert(start);
assert(start->self);
assert(start->name);
assert(start->value);
assert(name);
headerlst_entry = start->self;
while (headerlst_entry) {
HTTPHeader_t* headerlst_value;
char* header_name;
headerlst_value = sclist_getvalue(headerlst_entry);
assert(headerlst_value);
header_name = HTTPHeader_getname(headerlst_value);
if (0==strcmp(header_name,name))
break;
headerlst_entry=sclist_nextrecord(headerlst_entry);
}
if (!headerlst_entry)
/* Not found */
return NULL;
else
/* Found */
return sclist_getvalue(headerlst_entry);
}
HTTP_t* HTTP_remheader(HTTP_t* http, HTTPHeader_t* header) {<--- The function 'HTTP_remheader' is never used.
assert(http);
assert(http->headers);
assert(http->body);
assert(header);
assert(header->self);
assert(header->name);
assert(header->value);
/* Remove from the header list */
sclist_remrecord(http->headers, header->self);
/** @todo: make sclist_remrecord return a status (found/notfound) and use it */
#if 0
if (NULL==sclist_remrecord(http->headers, header->sclistrecord)) {
perror("HTTP_addheader");
return NULL;
}
#endif
header->self = NULL;
HTTPHeader_del(header);
return http;
}
HTTP_t* HTTP_new() {
HTTP_t* http;
if (NULL==(http=malloc(sizeof(struct HTTP_s)))) {
perror("HTTP_new HTTP_s");
return NULL;
}
if (NULL==(http->body=strdup(""))) {
perror("HTTP_new body");
free(http);
return NULL;
};
if (NULL==(http->headers = sclist_new())) {
perror("HTTP_new headers");
free(http->body);
free(http);
return NULL;
}
return http;
}
void HTTP_del(HTTP_t* http) {
sclistrecord_t* headerlst_entry;
assert(http);
assert(http->headers);
assert(http->body);
headerlst_entry = sclist_firstrecord(http->headers);
while (headerlst_entry) {
HTTPHeader_t* headerlst_value;
/* Free payload */
headerlst_value=sclist_getvalue(headerlst_entry);
HTTPHeader_del(headerlst_value);
/* Remember the entry to delete before moving to the next one */
sclistrecord_t* tmp = headerlst_entry;
headerlst_entry = sclist_nextrecord(headerlst_entry);
sclist_remrecord(http->headers,tmp);
}
free(http->body);
free(http);
}
static char* strconcat(char* dst, char* src) {
char* newdst;
assert(dst);
assert(src);
if (NULL==(newdst=realloc(dst,strlen(dst)+strlen(src)+1))) {
perror("strconcat");
return NULL;
}
dst = newdst;
strcat(dst,src);
return dst;
}
char* HTTP_buildheaders(const HTTP_t* http) {
char* headers_str=NULL;
sclistrecord_t* headerlst_entry;
HTTPHeader_t* headerlst_value;<--- The scope of the variable 'headerlst_value' can be reduced. [+]The scope of the variable 'headerlst_value' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
assert(http);
assert(http->headers);
assert(http->body);
if (NULL==(headers_str=strdup(""))) {
perror("HTTP_buildheaders headers_str");
return NULL;
}
for (headerlst_entry=sclist_firstrecord(http->headers); headerlst_entry; headerlst_entry = sclist_nextrecord(headerlst_entry)) {
headerlst_value = sclist_getvalue(headerlst_entry);
assert(headerlst_value);
/** @todo: OOM tests */
headers_str=strconcat(headers_str,HTTPHeader_getname(headerlst_value));
headers_str=strconcat(headers_str,": ");
headers_str=strconcat(headers_str,HTTPHeader_getvalue(headerlst_value));
headers_str=strconcat(headers_str,"\r\n");
}
return headers_str;
}
char* HTTP_buildrequest(const HTTPMethod_t method, const char* uri, const HTTPVersion_t version) {
char* retval;
assert(method<=HTTPMETHOD_INVALID);
assert(uri);
assert(version<=HTTPVERSION_INVALID);
if (NULL==(retval=malloc(7+1+strlen(uri)+1+8+1))) {
perror("HTTP_getrequest");
exit(EXIT_FAILURE);
}
retval[0]=0;
switch(method) {
case HTTPMETHOD_GET:
strcat(retval, "GET");
break;;
case HTTPMETHOD_HEAD:
case HTTPMETHOD_POST:
case HTTPMETHOD_PUT:
case HTTPMETHOD_DELETE:
case HTTPMETHOD_CONNECT:
case HTTPMETHOD_OPTIONS:
case HTTPMETHOD_TRACE:
case HTTPMETHOD_PATCH:
case HTTPMETHOD_INVALID:
default:
fprintf(stderr,"HTTP method not supported\n");
exit(EXIT_FAILURE);
break;;
}
if (uri[0]) {
strcat(retval," ");
strcat(retval, uri);
}
strcat(retval," HTTP/");
switch(version) {
case HTTPVERSION_HTTP11:
case HTTPVERSION_HTTP11b:
strcat(retval, "1.1");
break;;
case HTTPVERSION_HTTP09:
case HTTPVERSION_HTTP10:
case HTTPVERSION_HTTP2:
case HTTPVERSION_HTTP3:
case HTTPVERSION_INVALID:
default:
fprintf(stderr,"HTTP version not supported\n");
exit(EXIT_FAILURE);
break;;
}
strcat(retval, "\r\n");
return retval;
}
|