You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Invalid or incomplete multibyte or wide character #225
if you connect to a lua-http server and send it unix newlines (which I'm aware is invalid), it will throw with a somewhat cryptic error, rather than returning an error value or tolerating it.
lua: repro.lua:13: /usr/local/share/lua/5.4/http/server.lua:182: get_next_incoming_stream: fill: Invalid or incomplete multibyte or wide character
stack traceback:
[C]: in function 'assert'
repro.lua:13: in main chunk
[C]: in ?
lua-http generally doesn't return any error messages for such calls, only errno values (see https://en.wikipedia.org/wiki/Errno.h, EILSEQ seemed the most appropriate to me for the scenario of expecting one byte sequence but receiving another.
sure, I suppose \r\n is a "multibyte sequence" strictly speaking, but the error made me thought I was sending invalid UTF-8 somehow or something. maybe EPROTO would be more appropriate. but I don't feel particularly strongly about that. this isn't the actual bug.
it does return an errno as expected. but then somewhere along the line after I return from onstream, it throws an error.
onstream=function(server, stream)
localheaders, err=stream:get_headers()
ifheadersthenstream:write_body_from_string("hello, world!")
elseprint("oh no, an error occurred. allow me to handle it properly.")
print("I surely don't need to crash.")
endend
oh no, an error occurred. allow me to handle it properly.
I surely don't need to crash.
lua: repro.lua:16: /usr/local/share/lua/5.4/http/server.lua:182: get_next_incoming_stream: fill: Invalid or incomplete multibyte or wide character
stack traceback:
[C]: in function 'assert'
repro.lua:16: in main chunk
[C]: in ?
while I appreciate that this does technically solve the problem, I intentionally do not use this feature because I think it is an antipattern. there's a difference between illegal characters in an HTTP request and dividing by zero. it's like on error resume next or setting a handler for SIGSEGV, though admittedly a bit less problematic. I want the program to stop when it faults, but not when it is in a situation that is expected to arise in the use of the program.
I was under the impression that I could handle error conditions in this library by checking the return values and that it would not raise errors in the case of e.g. invalid input.
That's what the context argument in onerror is for: in the case of this error, it's a connection level error, so the context is the connection object.
You get to decide what you consider fatal vs ignorable.
Anything that is fatal to the whole sever is returned as an error from :loop()
If you look at the examples you'll see that onerror often just logs the error and continues:
Activity
daurnimator commentedon Aug 7, 2024
This is your operating system's standard error message when a function returns
EILSEQ. Probably fromlua-http/http/h1_connection.lua
Line 172 in ddab283
lua-http generally doesn't return any error messages for such calls, only errno values (see https://en.wikipedia.org/wiki/Errno.h,
EILSEQseemed the most appropriate to me for the scenario of expecting one byte sequence but receiving another.les-citrons commentedon Aug 7, 2024
sure, I suppose
\r\nis a "multibyte sequence" strictly speaking, but the error made me thought I was sending invalid UTF-8 somehow or something. maybeEPROTOwould be more appropriate. but I don't feel particularly strongly about that. this isn't the actual bug.it does return an errno as expected. but then somewhere along the line after I return from onstream, it throws an error.
daurnimator commentedon Aug 7, 2024
Add an
onerror: https://daurnimator.github.io/lua-http/0.4/#http.server:onerrorles-citrons commentedon Aug 8, 2024
while I appreciate that this does technically solve the problem, I intentionally do not use this feature because I think it is an antipattern. there's a difference between illegal characters in an HTTP request and dividing by zero. it's like
on error resume nextor setting a handler forSIGSEGV, though admittedly a bit less problematic. I want the program to stop when it faults, but not when it is in a situation that is expected to arise in the use of the program.I was under the impression that I could handle error conditions in this library by checking the return values and that it would not raise errors in the case of e.g. invalid input.
daurnimator commentedon Aug 8, 2024
That's what the
contextargument inonerroris for: in the case of this error, it's a connection level error, so the context is the connection object.You get to decide what you consider fatal vs ignorable.
Anything that is fatal to the whole sever is returned as an error from
:loop()If you look at the examples you'll see that
onerroroften just logs the error and continues:lua-http/examples/server_hello.lua
Lines 46 to 51 in ddab283
les-citrons commentedon Aug 8, 2024
ok, I see. so, how do I distinguish I/O errors from normal lua errors with the context argument?
edit:
this is what I'd like to avoid doing.