@@ -93,11 +93,13 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
93
93
hc = mr -> http_connection ;
94
94
c = mr -> connection ;
95
95
96
- #if 0
96
+ #if 1
97
97
dd ("hc->nbusy: %d" , (int ) hc -> nbusy );
98
98
99
- dd ("hc->busy: %p %p %p %p" , hc -> busy [0 ]-> start , hc -> busy [0 ]-> pos ,
100
- hc -> busy [0 ]-> last , hc -> busy [0 ]-> end );
99
+ if (hc -> nbusy ) {
100
+ dd ("hc->busy: %p %p %p %p" , hc -> busy [0 ]-> start , hc -> busy [0 ]-> pos ,
101
+ hc -> busy [0 ]-> last , hc -> busy [0 ]-> end );
102
+ }
101
103
102
104
dd ("request line: %p %p" , mr -> request_line .data ,
103
105
mr -> request_line .data + mr -> request_line .len );
@@ -120,7 +122,7 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
120
122
first = b ;
121
123
122
124
if (mr -> header_in == b ) {
123
- size += mr -> header_end + 2 - mr -> request_line .data ;
125
+ size += mr -> header_in -> pos - mr -> request_line .data ;
124
126
125
127
} else {
126
128
/* the subsequent part of the header is in the large header
@@ -138,6 +140,8 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
138
140
}
139
141
}
140
142
143
+ dd ("size: %d" , (int ) size );
144
+
141
145
if (hc -> nbusy ) {
142
146
b = NULL ;
143
147
for (i = 0 ; i < hc -> nbusy ; i ++ ) {
@@ -160,21 +164,26 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
160
164
}
161
165
162
166
if (b == mr -> header_in ) {
163
- size += mr -> header_end + 2 - b -> start ;
167
+ size += mr -> header_in -> pos - b -> start ;
164
168
break ;
165
169
}
166
170
167
171
size += b -> pos - b -> start ;
168
172
}
169
173
}
170
174
175
+ size ++ ; /* plus the null terminator, as required by the later
176
+ ngx_strstr() call */
177
+
178
+ dd ("header size: %d" , (int ) size );
179
+
171
180
data = lua_newuserdata (L , size );
172
181
last = data ;
173
182
174
183
b = c -> buffer ;
175
184
if (first == b ) {
176
185
if (mr -> header_in == b ) {
177
- pos = mr -> header_end + 2 ;
186
+ pos = mr -> header_in -> pos ;
178
187
179
188
} else {
180
189
pos = b -> pos ;
@@ -221,7 +230,7 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
221
230
p = last ;
222
231
223
232
if (b == mr -> header_in ) {
224
- pos = mr -> header_end + 2 ;
233
+ pos = mr -> header_in -> pos ;
225
234
226
235
} else {
227
236
pos = b -> pos ;
@@ -277,10 +286,22 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
277
286
}
278
287
}
279
288
289
+ * last ++ = '\0' ;
290
+
280
291
if (last - data > (ssize_t ) size ) {
281
292
return luaL_error (L , "buffer error: %d" , (int ) (last - data - size ));
282
293
}
283
294
295
+ /* strip the leading part (if any) of the request body in our header.
296
+ * the first part of the request body could slip in because nginx core's
297
+ * ngx_http_request_body_length_filter and etc can move r->header_in->pos
298
+ * in case that some of the body data has been preread into r->header_in.
299
+ */
300
+ p = (u_char * ) ngx_strstr (data , CRLF CRLF );
301
+ if (p ) {
302
+ last = p + sizeof (CRLF CRLF ) - 1 ;
303
+ }
304
+
284
305
lua_pushlstring (L , (char * ) data , last - data );
285
306
return 1 ;
286
307
}
0 commit comments