Software /
code /
prosody
Annotate
spec/net_http_parser_spec.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
author | Martijn van Duren <martijn@openbsd.org> |
---|---|
date | Thu, 06 Feb 2025 15:04:38 +0000 |
parent | 13378:db30ffbf2090 |
rev | line source |
---|---|
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
1 local http_parser = require "net.http.parser"; |
11030
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
2 local sha1 = require "util.hashes".sha1; |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
3 |
11033
cb5555443852
net.http.parser: Allow configuration of the chunk size fed to the parser
Matthew Wild <mwild1@gmail.com>
parents:
11032
diff
changeset
|
4 local parser_input_bytes = 3; |
cb5555443852
net.http.parser: Allow configuration of the chunk size fed to the parser
Matthew Wild <mwild1@gmail.com>
parents:
11032
diff
changeset
|
5 |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
6 local function CRLF(s) |
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
7 return (s:gsub("\n", "\r\n")); |
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
8 end |
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
9 |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
10 local function test_stream(stream, expect) |
12889
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
11 local chunks_processed = 0; |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
12 local success_cb = spy.new(function (packet) |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
13 assert.is_table(packet); |
11021
9673c95895fb
net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents:
10497
diff
changeset
|
14 if packet.body ~= false then |
9673c95895fb
net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents:
10497
diff
changeset
|
15 assert.is_equal(expect.body, packet.body); |
9673c95895fb
net.http.parser: Allow specifying sink for large request bodies
Kim Alvefur <zash@zash.se>
parents:
10497
diff
changeset
|
16 end |
12889
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
17 if expect.chunks then |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
18 if chunks_processed == 0 then |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
19 assert.is_true(packet.partial); |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
20 packet.body_sink = { |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
21 write = function (_, data) |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
22 chunks_processed = chunks_processed + 1; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
23 assert.equal(expect.chunks[chunks_processed], data); |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
24 return true; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
25 end; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
26 }; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
27 end |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
28 end |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
29 end); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
30 |
12889
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
31 local function options_cb() |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
32 return { |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
33 -- Force streaming API mode |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
34 body_size_limit = expect.chunks and 0 or nil; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
35 buffer_size_limit = 10*1024*2; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
36 }; |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
37 end |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
38 |
12889
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
39 local parser = http_parser.new(success_cb, error, (stream[1] or stream):sub(1,4) == "HTTP" and "client" or "server", options_cb) |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
40 if type(stream) == "string" then |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
41 for chunk in stream:gmatch("."..string.rep(".?", parser_input_bytes-1)) do |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
42 parser:feed(chunk); |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
43 end |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
44 else |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
45 for _, chunk in ipairs(stream) do |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
46 parser:feed(chunk); |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
47 end |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
48 end |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
49 |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
50 if expect.chunks then |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
51 assert.equal(chunks_processed, #expect.chunks); |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
52 end |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
53 assert.spy(success_cb).was_called(expect.count or 1); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
54 end |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
55 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
56 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
57 describe("net.http.parser", function() |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
58 describe("parser", function() |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
59 it("should handle requests with no content-length or body", function () |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
60 test_stream( |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
61 CRLF[[ |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 GET / HTTP/1.1 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 Host: example.com |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
65 ]], |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
66 { |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
67 body = ""; |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
68 } |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
69 ); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
70 end); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
71 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
72 it("should handle responses with empty body", function () |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
73 test_stream( |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
74 CRLF[[ |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 HTTP/1.1 200 OK |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 Content-Length: 0 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
78 ]], |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
79 { |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
80 body = ""; |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
81 } |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
82 ); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
83 end); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
84 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
85 it("should handle simple responses", function () |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
86 test_stream( |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
87 |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
88 CRLF[[ |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 HTTP/1.1 200 OK |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 Content-Length: 7 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 Hello |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
93 ]], |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
94 { |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
95 body = "Hello\r\n", count = 1; |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
96 } |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
97 ); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
98 end); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
99 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
100 it("should handle chunked encoding in responses", function () |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
101 test_stream( |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
102 |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
103 CRLF[[ |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
104 HTTP/1.1 200 OK |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
105 Transfer-Encoding: chunked |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
106 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
107 1 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
108 H |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
109 1 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
110 e |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
111 2 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
112 ll |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
113 1 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
114 o |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
115 0 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
116 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
117 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
118 ]], |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
119 { |
12882
9ed628635dc6
net.http.parser: Improve handling of responses without content-length
Matthew Wild <mwild1@gmail.com>
parents:
11033
diff
changeset
|
120 body = "Hello", count = 3; |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
121 } |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
122 ); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
123 end); |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
124 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
125 it("should handle a stream of responses", function () |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
126 test_stream( |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
127 |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
128 CRLF[[ |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
129 HTTP/1.1 200 OK |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
130 Content-Length: 5 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
131 |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
132 Hello |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 HTTP/1.1 200 OK |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 Transfer-Encoding: chunked |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 1 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 H |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 1 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 e |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 2 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 ll |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 1 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 o |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 0 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
147 ]], |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
148 { |
12882
9ed628635dc6
net.http.parser: Improve handling of responses without content-length
Matthew Wild <mwild1@gmail.com>
parents:
11033
diff
changeset
|
149 body = "Hello", count = 4; |
10497
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
150 } |
a9fb553b6dbb
net.http.parser tests: Expand tests to include validation of results
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
151 ); |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 end); |
12889
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
153 |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
154 it("should correctly find chunk boundaries", function () |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
155 test_stream({ |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
156 |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
157 CRLF[[ |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
158 HTTP/1.1 200 OK |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
159 Transfer-Encoding: chunked |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
160 |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
161 ]].."3\r\n:)\n\r\n"}, |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
162 { |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
163 count = 1; -- Once (partial) |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
164 chunks = { |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
165 ":)\n" |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
166 }; |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
167 } |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
168 ); |
94a99330ce87
net.http.parser: Fix off-by-one error in chunk parser
Matthew Wild <mwild1@gmail.com>
parents:
12882
diff
changeset
|
169 end); |
13378
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
170 |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
171 it("should reject very large request heads", function() |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
172 local finished = false; |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
173 local success_cb = spy.new(function() |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
174 finished = true; |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
175 end) |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
176 local error_cb = spy.new(function() |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
177 finished = true; |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
178 end) |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
179 local parser = http_parser.new(success_cb, error_cb, "server", function() |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
180 return { head_size_limit = 1024; body_size_limit = 1024; buffer_size_limit = 2048 }; |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
181 end) |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
182 parser:feed("GET / HTTP/1.1\r\n"); |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
183 for i = 1, 64 do -- * header line > buffer_size_limit |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
184 parser:feed(string.format("Header-%04d: Yet-AnotherValue\r\n", i)); |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
185 if finished then |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
186 -- should hit an error around half-way |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
187 break |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
188 end |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
189 end |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
190 if not finished then |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
191 parser:feed("\r\n") |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
192 end |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
193 assert.spy(success_cb).was_called(0); |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
194 assert.spy(error_cb).was_called(1); |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
195 assert.spy(error_cb).was_called_with("header-too-large"); |
db30ffbf2090
net.http.parser: Reject overlarge header section earlier
Kim Alvefur <zash@zash.se>
parents:
12889
diff
changeset
|
196 end) |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 end); |
11030
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
198 |
11032
28de68414750
net.http.parser: Switch tests so that CRLF conversion of input data is optional
Matthew Wild <mwild1@gmail.com>
parents:
11031
diff
changeset
|
199 it("should handle large chunked responses", function () |
11031
57739c591a8c
net.http.parser: Fix incorrect path in test
Matthew Wild <mwild1@gmail.com>
parents:
11030
diff
changeset
|
200 local data = io.open("spec/inputs/http/httpstream-chunked-test.txt", "rb"):read("*a"); |
11030
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
201 |
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
202 -- Just a sanity check... text editors and things may mess with line endings, etc. |
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
203 assert.equal("25930f021785ae14053a322c2dbc1897c3769720", sha1(data, true), "test data malformed"); |
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
204 |
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
205 test_stream(data, { |
12882
9ed628635dc6
net.http.parser: Improve handling of responses without content-length
Matthew Wild <mwild1@gmail.com>
parents:
11033
diff
changeset
|
206 body = string.rep("~", 11085), count = 3; |
11030
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
207 }); |
388f599f66d1
net.http.parser: Add failing test for (large?) chunk-encoded responses
Matthew Wild <mwild1@gmail.com>
parents:
11021
diff
changeset
|
208 end); |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 end); |