-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.utils.c
156 lines (119 loc) · 2.66 KB
/
http.utils.c
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
#ifdef __ORCAC__
#pragma optimize 79
#pragma noroot
#endif
#include <ctype.h>
#include "url.h"
int parseHeaderLine(const char *cp, unsigned length, URLRange *key, URLRange *value)
{
unsigned i, l;
key->location = 0;
key->length = 0;
value->location = 0;
value->length = 0;
// trim any trailing whitespace.
while (length)
{
if (isspace(cp[length - 1]))
--length;
else break;
}
if (!length) return 0;
/* format:
* key: value
* /^([^:]+):\s+(.*)$/
* -or-
* value [continuation of previous line]
* /^\s+(.*)$/
*/
for (i = 0; i < length; ++i)
{
if (cp[i] == ':') break;
}
if (i == length)
{
// try as value only
i = 0;
}
else
{
key->length = i;
i = i + 1;
}
// now gobble up all the whitespace...
for ( ; i < length; ++i)
{
if (!isspace(cp[i])) break;
}
// no value? no problem!
if (i == length) return 1;
value->location = i;
value->length = length - i;
return 1;
}
int parseStatusLine(const char *cp, unsigned length, int *version, int *status)
{
/*
* HTTP/1.1 200 OK etc.
*
*/
unsigned short *wp;
int i;
char c;
int x;
*version = 0;
*status = 0;
wp = (unsigned short *)cp;
// HTTP/
if (length <= 5) return 0;
if ((wp[0] | 0x2020) != 0x7468) return 0; // 'ht'
if ((wp[1] | 0x2020) != 0x7074) return 0; // 'tp'
if (cp[4] != '/') return 0;
// version string.
// \d+ . \d+
i = 5;
c = cp[i];
if (!isdigit(c)) return 0;
x = c - '0';
for (i = i + 1; i < length; ++i)
{
c = cp[i];
if (!isdigit(c)) break;
x = (x << 1) + (x << 3) + (c - '0');
}
*version = x << 8;
if (i == length) return 0;
if (cp[i++] != '.') return 0;
c = cp[i];
if (!isdigit(c)) return 0;
x = c - '0';
for (i = i + 1; i < length; ++i)
{
c = cp[i];
if (!isdigit(c)) break;
x = (x << 1) + (x << 3) + (c - '0');
}
*version |= x;
// 1+ space
if (i == length) return 0;
c = cp[i];
if (!isspace(c)) return 0;
for (i = i + 1; i < length; ++i)
{
c = cp[i];
if (!isspace(c)) break;
}
if (i == length) return 0;
c = cp[i];
if (!isdigit(c)) return 0;
x = c - '0';
for (i = i + 1; i < length; ++i)
{
c = cp[i];
if (!isdigit(c)) break;
x = (x << 1) + (x << 3) + (c - '0');
}
*status = x;
// rest of status line unimportant.
return 1;
}