Repository navigation
Expand file tree
/
Copy pathhttp.c
More file actions
106 lines (75 loc) · 2.98 KB
/
Copy pathhttp.c
File metadata and controls
106 lines (75 loc) · 2.98 KB
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
#include "http.h"
#include <string.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#else
#endif
struct http* http_reset(struct http* p)
{
p->m_a_[0] = 0;
p->m_a_[1] = 0;
p->m_a_[2] = 0;
p->m_a_[3] = 0;
p->m_a__[0] = 0;
p->m_a__[1] = 0;
p->m_pMethod = NULL;
p->m_pURI = NULL;
p->m_pVersion = NULL;
p->m_nHeaders = 0;
return p;
}
int http_parse(struct http* p, int len){
int i=0;
p->m_pMethod = &p->m_aBuf[i];
while(i<len && p->m_aBuf[i]!=' ') ++i;
if(len<=i) goto emergency;
p->m_aBuf[i] = '\0';
i += 1;
p->m_pURI = &p->m_aBuf[i];
while(i<len && p->m_aBuf[i]!=' ') ++i;
if(len<=i) goto emergency;
p->m_aBuf[i] = '\0';
i += 1;
p->m_pVersion = &p->m_aBuf[i];
while(i<len && p->m_aBuf[i]!='\r') ++i;
if(len<=i) goto emergency;
p->m_aBuf[i] = '\0';
i += 2;
p->m_nHeaders = 0;
while(i<len && p->m_aBuf[i]!='\r'){
// NOT SUPPORTED folded header.
if(p->m_aBuf[i]==' ' || p->m_aBuf[i]=='\t') goto emergency;
p->m_aHeaderN[p->m_nHeaders] = &p->m_aBuf[i];
while(i<len && p->m_aBuf[i]!=':') ++i;
if(len<=i) goto emergency;
p->m_aBuf[i] = '\0';
i += 1;
while(i<len && p->m_aBuf[i]==' ') ++i;
p->m_aHeaderV[p->m_nHeaders] = &p->m_aBuf[i];
while(i<len && p->m_aBuf[i]!='\r') ++i;
if(len<=i) goto emergency;
p->m_aBuf[i] = '\0';
i += 2;
++p->m_nHeaders;
if(HTTP_HEADMAX <= p->m_nHeaders) goto emergency;
}
if(len<=i) goto emergency;
p->m_aBuf[i] = '\0';
i += 2; // This is safe, because m_aBuf is larger than HTTP_BUFSIZE 2bytes.
return i;
emergency:
p->m_pMethod = NULL;
p->m_pURI = NULL;
p->m_pVersion = NULL;
p->m_nHeaders = 0;
return -i;
}
const char* http_header(struct http* p, const char* pName){
int i;
for(i=0; i<p->m_nHeaders; ++i){
if(strcasecmp(p->m_aHeaderN[i], pName) == 0){
return p->m_aHeaderV[i]; // NOT SUPPORTED same named header.
}
}
return "";
}