From 35c3a1b7150305730491ee1307a86bb536aabac3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 12:45:25 +0300 Subject: [PATCH] all: should rename picohttpparser to pico_http_parser in vlib? (fixes #16151) --- vlib/pico_http_parser/pico_http_parser.v | 15 ++++++++ vlib/pico_http_parser/pico_http_parser_test.v | 34 +++++++++++++++++++ vlib/picoev/picoev.v | 18 +++++----- vlib/picoev/socket_util.c.v | 4 +-- vlib/picohttpparser/README.md | 5 ++- 5 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 vlib/pico_http_parser/pico_http_parser.v create mode 100644 vlib/pico_http_parser/pico_http_parser_test.v diff --git a/vlib/pico_http_parser/pico_http_parser.v b/vlib/pico_http_parser/pico_http_parser.v new file mode 100644 index 000000000..487f5c940 --- /dev/null +++ b/vlib/pico_http_parser/pico_http_parser.v @@ -0,0 +1,15 @@ +module pico_http_parser + +import picohttpparser + +pub type Header = picohttpparser.Header + +pub type Request = picohttpparser.Request + +pub type Response = picohttpparser.Response + +// u64toa forwards to `picohttpparser.u64toa` for backwards-compatible imports. +@[unsafe] +pub fn u64toa(buf_start &u8, value u64) !int { + return picohttpparser.u64toa(buf_start, value) +} diff --git a/vlib/pico_http_parser/pico_http_parser_test.v b/vlib/pico_http_parser/pico_http_parser_test.v new file mode 100644 index 000000000..4c62126f8 --- /dev/null +++ b/vlib/pico_http_parser/pico_http_parser_test.v @@ -0,0 +1,34 @@ +module pico_http_parser + +pub fn test_request_alias_parses_a_simple_get_request() { + mut req := Request{} + parsed := req.parse_request('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n') or { + assert false, 'error while parse request: ${err}' + 0 + } + + assert parsed == 37 + assert req.method == 'GET' + assert req.path == '/' + assert req.headers[0].name == 'Host' + assert req.headers[0].value == 'example.com' +} + +pub fn test_u64toa_alias_formats_numbers() { + mut buf := [10]u8{} + len := unsafe { + u64toa(&buf[0], 12345) or { + assert false, 'error while formatting number: ${err}' + 0 + } + } + + assert len == 5 + assert buf[0..len] == '12345'.bytes() +} + +pub fn test_response_alias_can_be_initialized() { + response := Response{} + + assert response.fd == 0 +} diff --git a/vlib/picoev/picoev.v b/vlib/picoev/picoev.v index 5e3e6b76c..6085363e9 100644 --- a/vlib/picoev/picoev.v +++ b/vlib/picoev/picoev.v @@ -1,7 +1,7 @@ module picoev import net -import picohttpparser +import pico_http_parser import time // maximum size of the event queue. @@ -40,8 +40,8 @@ pub mut: pub struct Config { pub: port int = 8080 - cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil } - err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_error_callback + cb fn (voidptr, pico_http_parser.Request, mut pico_http_parser.Response) = unsafe { nil } + err_cb fn (voidptr, pico_http_parser.Request, mut pico_http_parser.Response, IError) = default_error_callback raw_cb fn (mut Picoev, int, int) = unsafe { nil } user_data voidptr = unsafe { nil } timeout_secs int = 8 @@ -56,8 +56,8 @@ pub: // Contains event loop, file descriptor table, timeouts, buffers, and configuration. @[heap] pub struct Picoev { - cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil } - error_callback fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_error_callback + cb fn (voidptr, pico_http_parser.Request, mut pico_http_parser.Response) = unsafe { nil } + error_callback fn (voidptr, pico_http_parser.Request, mut pico_http_parser.Response, IError) = default_error_callback raw_callback fn (mut Picoev, int, int) = unsafe { nil } timeout_secs int @@ -195,7 +195,7 @@ fn accept_callback(listen_fd int, _events int, cb_arg voidptr) { trace_fd('accept ${accepted_fd}') setup_sock(accepted_fd) or { elog('setup_sock failed, fd: ${accepted_fd}, listen_fd: ${listen_fd}, err: ${err.code()}') - pv.error_callback(pv.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{}, + pv.error_callback(pv.user_data, pico_http_parser.Request{}, mut &pico_http_parser.Response{}, err) close_socket(accepted_fd) // Close fd on failure return @@ -237,13 +237,13 @@ fn raw_callback(fd int, events int, context voidptr) { unsafe { request_buffer += fd * pv.max_read // pointer magic } - mut req := picohttpparser.Request{} + mut req := pico_http_parser.Request{} // Response init mut response_buffer := pv.out unsafe { response_buffer += fd * pv.max_write // pointer magic } - mut res := picohttpparser.Response{ + mut res := pico_http_parser.Response{ fd: fd buf_start: response_buffer buf: response_buffer @@ -293,7 +293,7 @@ fn raw_callback(fd int, events int, context voidptr) { } } -fn default_error_callback(_data voidptr, _req picohttpparser.Request, mut res picohttpparser.Response, error IError) { +fn default_error_callback(_data voidptr, _req pico_http_parser.Request, mut res pico_http_parser.Response, error IError) { elog('picoev: ${error}') res.end() } diff --git a/vlib/picoev/socket_util.c.v b/vlib/picoev/socket_util.c.v index c344e34fa..0815d5ae8 100644 --- a/vlib/picoev/socket_util.c.v +++ b/vlib/picoev/socket_util.c.v @@ -1,7 +1,7 @@ module picoev import net -import picohttpparser +import pico_http_parser #include $if windows { @@ -127,7 +127,7 @@ fn listen(config Config) !int { net.socket_error_message(C.listen(fd, C.SOMAXCONN), 'listening on ${saddr} with maximum backlog pending queue of ${C.SOMAXCONN}, failed')! setup_sock(fd) or { - config.err_cb(config.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{}, + config.err_cb(config.user_data, pico_http_parser.Request{}, mut &pico_http_parser.Response{}, err) } return fd diff --git a/vlib/picohttpparser/README.md b/vlib/picohttpparser/README.md index f557f1dfb..7d749500d 100644 --- a/vlib/picohttpparser/README.md +++ b/vlib/picohttpparser/README.md @@ -1,5 +1,8 @@ ## Description -`picohttpparser` is V implementation of +Prefer `import pico_http_parser` for new code. The original +`import picohttpparser` path remains available for compatibility. + +`picohttpparser` is V implementation of [picohttpparser](https://github.com/h2o/picohttpparser), which in turn is "a tiny, primitive, fast HTTP request/response parser." -- 2.39.5