Skip to content

Commit 2152db2

Browse files
committed
#58 - fixed
1 parent a3237d3 commit 2152db2

File tree

5 files changed

+97
-18
lines changed

5 files changed

+97
-18
lines changed

src/ngx_http_tnt_handlers.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ngx_http_tnt_get_output_size(
4949

5050
if (r->method & NGX_HTTP_POST ||
5151
((r->method & NGX_HTTP_PUT || r->method & NGX_HTTP_DELETE)
52-
&& r->headers_in.content_length_n))
52+
&& r->headers_in.content_length_n > 0))
5353
{
5454
output_size += r->headers_in.content_length_n;
5555
}
@@ -564,22 +564,11 @@ ngx_int_t ngx_http_tnt_init_handlers(
564564
u->abort_request = ngx_http_tnt_abort_request;
565565
u->finalize_request = ngx_http_tnt_finalize_request;
566566

567-
switch (r->method) {
568-
case NGX_HTTP_POST:
569-
if (r->headers_in.content_length_n == 0) {
570-
return NGX_HTTP_BAD_REQUEST;
571-
}
567+
568+
if (r->headers_in.content_length_n > 0) {
572569
u->create_request = ngx_http_tnt_body_json_handler;
573-
break;
574-
default:
575-
if ((r->method == NGX_HTTP_PUT || r->method == NGX_HTTP_DELETE)
576-
&& r->headers_in.content_length_n) {
577-
u->create_request = ngx_http_tnt_body_json_handler;
578-
}
579-
else {
580-
u->create_request = ngx_http_tnt_query_handler;
581-
}
582-
break;
570+
} else {
571+
u->create_request = ngx_http_tnt_query_handler;
583572
}
584573

585574
return NGX_OK;
@@ -618,7 +607,7 @@ ngx_http_tnt_body_json_handler(ngx_http_request_t *r)
618607
if (out_chain->buf == NULL) {
619608
crit("[BUG?] ngx_http_tnt_body_json_handler -- "
620609
"failed to allocate output buffer, size %ui",
621-
r->headers_in.content_length_n * tlcf->in_multiplier);
610+
(r->headers_in.content_length_n + 1) * tlcf->in_multiplier);
622611
return NGX_ERROR;
623612
}
624613

test/http_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,48 @@ def get_fail(url, data, headers):
199199
(code, msg) = get(url, data, headers)
200200
return (code, msg)
201201

202+
def delete(url, data, headers):
203+
out = '{}'
204+
try:
205+
req = urllib2.Request(url)
206+
req.get_method = lambda: 'DELETE'
207+
if headers:
208+
for header in headers:
209+
req.add_header(header, headers[header])
210+
if data:
211+
req.add_header('Content-Type', 'application/json')
212+
res = urllib2.urlopen(req, json.dumps(data))
213+
else:
214+
res = urllib2.urlopen(req)
215+
216+
out = res.read()
217+
out = out + res.read()
218+
rc = res.getcode()
219+
220+
if VERBOSE:
221+
print("code: ", rc, " recv: '", out, "'")
222+
223+
if rc != 500:
224+
return (rc, json.loads(out))
225+
226+
return (rc, False)
227+
except urllib2.HTTPError as e:
228+
if e.code == 400:
229+
out = e.read();
230+
231+
if VERBOSE:
232+
print("code: ", e.code, " recv: '", out, "'")
233+
234+
return (e.code, json.loads(out))
235+
except Exception as e:
236+
print(traceback.format_exc())
237+
return (False, e)
238+
239+
def delete_success(url, data, headers):
240+
(code, msg) = delete(url, data, headers)
241+
assert(code == 200), 'expected 200'
242+
result = get_result(msg)
243+
202244
def assert_headers(result, headers_in):
203245
for header in headers_in:
204246
header_from_server = result[0]['headers'][header]

test/ngx_confs/tnt_server_test.conf

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ http {
1717
send_timeout 10;
1818

1919
upstream tnt {
20-
server 127.0.0.1:9999 max_fails=1 fail_timeout=30s;
20+
server 127.0.0.1:9999 max_fails=1 fail_timeout=1s;
2121
keepalive 1;
2222
}
2323

@@ -148,5 +148,27 @@ http {
148148
tnt_pass tnt;
149149
}
150150
# ]]
151+
152+
# BUG -- https://github.com/tarantool/nginx_upstream_module/issues/58 [[
153+
location /issue_58 {
154+
if ( $request_method = GET ) {
155+
tnt_method "read";
156+
}
157+
if ( $request_method = PUT ) {
158+
tnt_method "insert";
159+
}
160+
if ( $request_method = POST ) {
161+
tnt_method "update";
162+
}
163+
if ( $request_method = DELETE ) {
164+
tnt_method "delete";
165+
}
166+
tnt_http_rest_methods get delete;
167+
tnt_http_methods all;
168+
tnt_pure_result off;
169+
tnt_pass_http_request on parse_args;
170+
tnt_pass tnt;
171+
}
172+
# ]]
151173
}
152174
}

test/test.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,17 @@ end
9292
function four_empty_strings()
9393
return {"", "", "", "", {a = ""}}
9494
end
95+
96+
function issue_58(request)
97+
return true
98+
end
99+
100+
-- BUG -- https://github.com/tarantool/nginx_upstream_module/issues/58
101+
function delete(request, a1, a2)
102+
return request, a1, a2
103+
end
104+
105+
function insert(request, a1, a2)
106+
return request, a1, a2
107+
end
108+
-- ]]

test/v23_features.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,15 @@
124124
preset_method_location = BASE_URL + '/tnt'
125125
post_success(preset_method_location, {
126126
'method':'four_empty_strings', 'params':[], 'id': 1}, {})
127+
128+
#===========
129+
#
130+
print('[+] issue #58, REST or RPC?')
131+
132+
preset_method_location = BASE_URL + '/issue_58'
133+
put_success(preset_method_location, None, None)
134+
put_success(preset_method_location, {'params':[1, 2]}, None)
135+
136+
delete_success(preset_method_location, None, None)
137+
delete_success(preset_method_location, {'params':[1, 2]}, None)
138+

0 commit comments

Comments
 (0)