-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.xs
143 lines (124 loc) · 3.59 KB
/
Parser.xs
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
#include "xshelper.h"
#include "picohttpparser/picohttpparser.c"
#define MAX_HEADERS 128
STATIC_INLINE char tou(char const ch)
{
return ('a' <= ch && ch <= 'z')
? ch - ('a' - 'A')
: ch;
}
STATIC_INLINE char tol(char const ch)
{
return ('A' <= ch && ch <= 'Z')
? ch - ('A' - 'a')
: ch;
}
MODULE = HTTP::Response::Parser PACKAGE = HTTP::Response::Parser::XS
PROTOTYPES: DISABLE
void parse_http_response(SV* buf, SV* resref, SV* option)
PPCODE:
{
const char* buf_str;
STRLEN buf_len;
const char* msg;
size_t msg_len;
int minor_version, status;
struct phr_header headers[MAX_HEADERS];
size_t num_headers;
size_t i;
int ret;
HV* res;
SV* last_value;
char tmp[1024];
HV* h_headers = newHV();
SV* ref = (SV*)newRV_noinc( (SV*)h_headers );
if ( SvROK(buf) ) {
buf_str = SvPV( SvRV(buf), buf_len);
} else {
buf_str = SvPV(buf, buf_len);
}
num_headers = MAX_HEADERS;
ret = phr_parse_response(buf_str, buf_len, &minor_version, &status, &msg, &msg_len, headers, &num_headers, 0);
if (ret == -1)
goto done;
if (!SvROK(resref))
Perl_croak(aTHX_ "second param to parse_http_response should be a hashref");
res = (HV*)SvRV(resref);
if (SvTYPE(res) != SVt_PVHV)
Perl_croak(aTHX_ "second param to parse_http_response should be a hashref");
// status line parsed
(void)hv_stores(res, "_protocol", newSVpvf("HTTP/1.%d", minor_version));
(void)hv_stores(res, "_rc", newSViv(status));
/* printf("status: %d\n", ret);
printf("msg_len: %d\n", msg_len);
printf("num_headers: %d\n", num_headers);
*/
(void)hv_stores(res, "_msg", newSVpvn(msg, msg_len));
// printf("hoge4\n");
last_value = NULL;
(void)hv_stores(res, "_headers", ref);
for (i = 0; i < num_headers; ++i) {
if (headers[i].name != NULL) {
const char* name;
size_t name_len;
SV** slot;
if (1) {
const char* s;
char* d;
size_t n;
// too large field name
if (sizeof(tmp) < headers[i].name_len) {
/*
printf("name_len: %d\n", headers[i].name_len);
printf("name: %s\n", headers[i].name);
*/
// hv_clear(res);
ret = -1;
goto done;
}
for (s = headers[i].name, n = headers[i].name_len, d = tmp;
n != 0;
s++, --n, d++)
*d = *s == '_' ? '-' : tol(*s);
name = tmp;
name_len = headers[i].name_len;
}
slot = hv_fetch(h_headers, name, name_len, TRUE);
if ( !slot )
croak("failed to create hash entry");
if (SvOK(*slot)) {
if (SvROK(*slot)) {
AV* values = (AV*)SvRV(*slot);
SV* newval = newSVpvn(headers[i].value, headers[i].value_len);
av_push(values, newval);
last_value = newval;
} else {
AV* values = newAV();
SV* newval = newSVpvn(headers[i].value, headers[i].value_len);
av_push(values, SvREFCNT_inc_simple_NN(*slot));
av_push(values, newval);
slot = hv_store(h_headers, name, name_len,
newRV_noinc((SV*)values), 0U);
last_value = newval;
}
} else {
sv_setpvn(*slot, headers[i].value, headers[i].value_len);
last_value = *slot;
}
} else {
/* continuing lines of a mulitiline header */
sv_catpvs(last_value, "\n");
sv_catpvn(last_value, headers[i].value, headers[i].value_len);
}
}
done:
if (SvTRUE(option)) {
EXTEND(SP, 4);
mPUSHi(ret);
mPUSHi(minor_version);
mPUSHi(status);
mPUSHp(msg, msg_len);
} else {
mPUSHi(ret);
}
}