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
|
#include "libhttpc.h"
#include <errno.h>
#include <stddef.h>
#include <string.h>
#ifdef LibHTTPC_SOCK
#include <sys/socket.h>
#include <sys/types.h>
#endif
struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_buf, char *buf)
{
char *next;
int selfmalloc = 0;
char *buf_end = strchr(buf, '\0');
if (!buf)
return NULL;
if (!request_buf)
{
if (!LibHTTPC_malloc || !LibHTTPC_realloc)
return NULL;
request_buf = LibHTTPC_malloc(sizeof(struct LibHTTPC_Request));
memset(request_buf, '\0', sizeof(struct LibHTTPC_Request));
request_buf->selfalloc = 1;
request_buf->header_count = -1;
}
if (request_buf->header_count == -1)
{
if (!LibHTTPC_malloc || !LibHTTPC_realloc)
return NULL;
selfmalloc = 1;
request_buf->header_selfalloc = 1;
request_buf->header_count = 0;
}
request_buf->method = request_buf->buf = buf;
next = strchr(request_buf->method, ' ');
if (!next)
return NULL;
*next = '\0';
request_buf->uri = next + 1;
next = strchr(request_buf->uri, ' ');
if (!next)
return NULL;
*next = '\0';
request_buf->version = next + 1;
next = strstr(request_buf->version, "\r\n");
if (!next)
return NULL;
*next++ = '\0';
if (next > buf_end)
return NULL;
for (size_t i = 0; (next = strstr(next, "\r\n")); ++next, ++i)
{
if (!next)
return NULL;
*next = '\0';
next += 2;
if (next > buf_end)
return NULL;
if (strstr(next, "\r\n") == next)
break;
if (selfmalloc)
{
if (!LibHTTPC_malloc || !LibHTTPC_realloc)
return NULL;
++request_buf->header_count;
if (!request_buf->headers)
request_buf->headers = LibHTTPC_malloc(sizeof(struct LibHTTPC_Header));
else
request_buf->headers = LibHTTPC_realloc(request_buf->headers, sizeof(struct LibHTTPC_Header));
}
if (i < request_buf->header_count)
{
request_buf->headers[i].name = next;
next = strchr(next, ':');
if (!next)
return NULL;
*next++ = '\0';
if (next > buf_end)
return NULL;
request_buf->headers[i].value = next;
}
}
request_buf->body = next + 2;
return request_buf;
}
#ifdef LibHTTPC_SOCK
struct LibHTTPC_Request *LibHTTPC_readRequest(
int sockfd,
struct LibHTTPC_Request *request_buf,
char *buf, size_t buf_len
)
{
if (!buf)
{
/* TODO: Implement behavior when buf == NULL */
return NULL;
} else
{
if (!buf_len)
return NULL;
ssize_t received = recv(sockfd, buf, buf_len, 0);
if (received < 0)
return NULL;
buf[buf_len - 1] = '\0';
return LibHTTPC_loadRequest(request_buf, buf);
}
}
#endif
int LibHTTPC_Request_(struct LibHTTPC_Request *request)
{
if (!LibHTTPC_free)
return -1;
if (request->header_selfalloc)
LibHTTPC_free(request->headers);
if (request->selfalloc)
LibHTTPC_free(request);
return errno;
}
|