picohttp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bba60b163b964308fafcdb14504076d28df6831bb2ebf80c9c3b8676f29f4734
4
+ data.tar.gz: 43ac8f3c3273fb49360d55d765c5c3ade646b3c503bb18eb23c211f7e4c4109a
5
+ SHA512:
6
+ metadata.gz: 65d914a80ec411ddc53b7e0982f6896f8f1315cd07a8daa01a50cc8d8183fefed315ee51525cef2131a28de00809ed5a1d719cebb5df0785bf03bc218bdc945d
7
+ data.tar.gz: 9229f345ba7d0945066dd60b81b3fbfbeb3c04e8e4ab1f7b7edc13636654d9f6be562ead8e1a5b3ac479f744342395e6d65f3e5a833522f7787e8fc161318df4
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 John Hawthorn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Picohttp
2
+
3
+ Fast HTTP request parser using picohttpparser.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add picohttp
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'picohttp'
17
+
18
+ request = "GET /api/users?limit=10 HTTP/1.1\r\n" +
19
+ "Host: api.example.com\r\n" +
20
+ "User-Agent: MyApp/1.0\r\n" +
21
+ "\r\n"
22
+
23
+ # Rack-compatible environment hash
24
+ env = Picohttp.parse_request_env(request)
25
+ # => {
26
+ # "REQUEST_METHOD" => "GET",
27
+ # "PATH_INFO" => "/api/users",
28
+ # "QUERY_STRING" => "limit=10",
29
+ # "SERVER_PROTOCOL" => "HTTP/1.1",
30
+ # "HTTP_HOST" => "api.example.com",
31
+ # "HTTP_USER_AGENT" => "MyApp/1.0"
32
+ # }
33
+ ```
34
+
35
+ Returns `nil` for incomplete requests, raises `Picohttp::ParseError` for invalid ones.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/picohttp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jhawthorn/picohttp/blob/main/CODE_OF_CONDUCT.md).
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
50
+
51
+ ## Code of Conduct
52
+
53
+ Everyone interacting in the Picohttp project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/picohttp/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rake/extensiontask"
9
+
10
+ task build: :compile
11
+
12
+ GEMSPEC = Gem::Specification.load("picohttp.gemspec")
13
+
14
+ Rake::ExtensionTask.new("picohttp", GEMSPEC) do |ext|
15
+ ext.lib_dir = "lib/picohttp"
16
+ end
17
+
18
+ task default: %i[clobber compile test]
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ # Makes all symbols private by default to avoid unintended conflict
6
+ # with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
7
+ # selectively, or entirely remove this flag.
8
+ append_cflags("-fvisibility=hidden")
9
+
10
+ # Check for Ractor support
11
+ have_func("rb_ext_ractor_safe", "ruby.h")
12
+
13
+ create_makefile("picohttp/picohttp")
@@ -0,0 +1,162 @@
1
+ #include "picohttp.h"
2
+ #include "picohttpparser.h"
3
+
4
+ #define MAX_HEADER_NAME_LEN 256
5
+
6
+ VALUE rb_mPicohttp;
7
+ VALUE rb_ePicohttpParseError;
8
+
9
+ // Frozen string constants
10
+ static VALUE rb_str_request_method;
11
+ static VALUE rb_str_server_protocol;
12
+ static VALUE rb_str_path_info;
13
+ static VALUE rb_str_query_string;
14
+ static VALUE rb_str_empty;
15
+
16
+ static VALUE
17
+ header_name_to_env_key(const char *name, size_t name_len)
18
+ {
19
+ if (name_len > MAX_HEADER_NAME_LEN) {
20
+ rb_raise(rb_ePicohttpParseError, "Header name too long");
21
+ }
22
+
23
+ char env_name[MAX_HEADER_NAME_LEN + 6]; // "HTTP_" + name + null terminator
24
+ strcpy(env_name, "HTTP_");
25
+
26
+ for (size_t j = 0; j < name_len; j++) {
27
+ char c = name[j];
28
+ if (c == '-') {
29
+ env_name[5 + j] = '_';
30
+ } else if (c >= 'a' && c <= 'z') {
31
+ env_name[5 + j] = c - 'a' + 'A';
32
+ } else {
33
+ env_name[5 + j] = c;
34
+ }
35
+ }
36
+ env_name[5 + name_len] = '\0';
37
+
38
+ return rb_interned_str(env_name, 5 + name_len);
39
+ }
40
+
41
+ static VALUE
42
+ picohttp_parse_request(VALUE self, VALUE str)
43
+ {
44
+ Check_Type(str, T_STRING);
45
+
46
+ const char *buf = RSTRING_PTR(str);
47
+ size_t len = RSTRING_LEN(str);
48
+
49
+ const char *method, *path;
50
+ int minor_version;
51
+ struct phr_header headers[100];
52
+ size_t method_len, path_len, num_headers = sizeof(headers) / sizeof(headers[0]);
53
+
54
+ int result = phr_parse_request(buf, len, &method, &method_len, &path, &path_len,
55
+ &minor_version, headers, &num_headers, 0);
56
+
57
+ if (result < 0) {
58
+ if (result == -2) {
59
+ return Qnil; // Incomplete request
60
+ }
61
+ rb_raise(rb_ePicohttpParseError, "Invalid HTTP request");
62
+ }
63
+
64
+ VALUE headers_hash = rb_hash_new();
65
+ for (size_t i = 0; i < num_headers; i++) {
66
+ if (headers[i].name == NULL) {
67
+ rb_raise(rb_ePicohttpParseError, "HTTP line folding not supported");
68
+ }
69
+ VALUE key = rb_str_new(headers[i].name, headers[i].name_len);
70
+ VALUE val = rb_str_new(headers[i].value, headers[i].value_len);
71
+ rb_hash_aset(headers_hash, key, val);
72
+ }
73
+
74
+ return rb_ary_new_from_args(5,
75
+ rb_str_new(method, method_len),
76
+ rb_str_new(path, path_len),
77
+ rb_sprintf("1.%d", minor_version),
78
+ headers_hash,
79
+ INT2FIX(result));
80
+ }
81
+
82
+ static VALUE
83
+ picohttp_parse_request_env(VALUE self, VALUE str)
84
+ {
85
+ Check_Type(str, T_STRING);
86
+
87
+ const char *buf = RSTRING_PTR(str);
88
+ size_t len = RSTRING_LEN(str);
89
+
90
+ const char *method, *path;
91
+ int minor_version;
92
+ struct phr_header headers[100];
93
+ size_t method_len, path_len, num_headers = sizeof(headers) / sizeof(headers[0]);
94
+
95
+ int result = phr_parse_request(buf, len, &method, &method_len, &path, &path_len,
96
+ &minor_version, headers, &num_headers, 0);
97
+
98
+ if (result < 0) {
99
+ if (result == -2) {
100
+ return Qnil; // Incomplete request
101
+ }
102
+ rb_raise(rb_ePicohttpParseError, "Invalid HTTP request");
103
+ }
104
+
105
+ VALUE env = rb_hash_new();
106
+
107
+ // Standard CGI/Rack environment variables
108
+ rb_hash_aset(env, rb_str_request_method, rb_str_new(method, method_len));
109
+ rb_hash_aset(env, rb_str_server_protocol, rb_sprintf("HTTP/1.%d", minor_version));
110
+
111
+ // Parse path and query string in C
112
+ const char *query_start = memchr(path, '?', path_len);
113
+ if (query_start) {
114
+ size_t path_info_len = query_start - path;
115
+ size_t query_len = path_len - path_info_len - 1;
116
+ rb_hash_aset(env, rb_str_path_info, rb_str_new(path, path_info_len));
117
+ rb_hash_aset(env, rb_str_query_string, rb_str_new(query_start + 1, query_len));
118
+ } else {
119
+ rb_hash_aset(env, rb_str_path_info, rb_str_new(path, path_len));
120
+ rb_hash_aset(env, rb_str_query_string, rb_str_empty);
121
+ }
122
+
123
+ // Convert headers to HTTP_ prefixed environment variables
124
+ for (size_t i = 0; i < num_headers; i++) {
125
+ if (headers[i].name == NULL) {
126
+ rb_raise(rb_ePicohttpParseError, "HTTP line folding not supported");
127
+ }
128
+
129
+ VALUE header_name = header_name_to_env_key(headers[i].name, headers[i].name_len);
130
+ VALUE header_value = rb_str_new(headers[i].value, headers[i].value_len);
131
+ rb_hash_aset(env, header_name, header_value);
132
+ }
133
+
134
+ return env;
135
+ }
136
+
137
+ RUBY_FUNC_EXPORTED void
138
+ Init_picohttp(void)
139
+ {
140
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
141
+ rb_ext_ractor_safe(true);
142
+ #endif
143
+
144
+ rb_mPicohttp = rb_define_module("Picohttp");
145
+ rb_ePicohttpParseError = rb_define_class_under(rb_mPicohttp, "ParseError", rb_eStandardError);
146
+ rb_define_module_function(rb_mPicohttp, "parse_request", picohttp_parse_request, 1);
147
+ rb_define_module_function(rb_mPicohttp, "parse_request_env", picohttp_parse_request_env, 1);
148
+
149
+ // Initialize interned string constants
150
+ rb_str_request_method = rb_interned_str_cstr("REQUEST_METHOD");
151
+ rb_str_server_protocol = rb_interned_str_cstr("SERVER_PROTOCOL");
152
+ rb_str_path_info = rb_interned_str_cstr("PATH_INFO");
153
+ rb_str_query_string = rb_interned_str_cstr("QUERY_STRING");
154
+ rb_str_empty = rb_interned_str_cstr("");
155
+
156
+ // Prevent garbage collection of constants
157
+ rb_gc_register_address(&rb_str_request_method);
158
+ rb_gc_register_address(&rb_str_server_protocol);
159
+ rb_gc_register_address(&rb_str_path_info);
160
+ rb_gc_register_address(&rb_str_query_string);
161
+ rb_gc_register_address(&rb_str_empty);
162
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef PICOHTTP_H
2
+ #define PICOHTTP_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* PICOHTTP_H */
@@ -0,0 +1,707 @@
1
+ /*
2
+ * Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
3
+ * Shigeo Mitsunari
4
+ *
5
+ * The software is licensed under either the MIT License (below) or the Perl
6
+ * license.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to
10
+ * deal in the Software without restriction, including without limitation the
11
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
+ * sell copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in
16
+ * all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
+ * IN THE SOFTWARE.
25
+ */
26
+
27
+ #include <assert.h>
28
+ #include <stddef.h>
29
+ #include <string.h>
30
+ #ifdef __SSE4_2__
31
+ #ifdef _MSC_VER
32
+ #include <nmmintrin.h>
33
+ #else
34
+ #include <x86intrin.h>
35
+ #endif
36
+ #endif
37
+ #include "picohttpparser.h"
38
+
39
+ #if __GNUC__ >= 3
40
+ #define likely(x) __builtin_expect(!!(x), 1)
41
+ #define unlikely(x) __builtin_expect(!!(x), 0)
42
+ #else
43
+ #define likely(x) (x)
44
+ #define unlikely(x) (x)
45
+ #endif
46
+
47
+ #ifdef _MSC_VER
48
+ #define ALIGNED(n) _declspec(align(n))
49
+ #else
50
+ #define ALIGNED(n) __attribute__((aligned(n)))
51
+ #endif
52
+
53
+ #define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u)
54
+
55
+ #define CHECK_EOF() \
56
+ if (buf == buf_end) { \
57
+ *ret = -2; \
58
+ return NULL; \
59
+ }
60
+
61
+ #define EXPECT_CHAR_NO_CHECK(ch) \
62
+ if (*buf++ != ch) { \
63
+ *ret = -1; \
64
+ return NULL; \
65
+ }
66
+
67
+ #define EXPECT_CHAR(ch) \
68
+ CHECK_EOF(); \
69
+ EXPECT_CHAR_NO_CHECK(ch);
70
+
71
+ #define ADVANCE_TOKEN(tok, toklen) \
72
+ do { \
73
+ const char *tok_start = buf; \
74
+ static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
75
+ int found2; \
76
+ buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
77
+ if (!found2) { \
78
+ CHECK_EOF(); \
79
+ } \
80
+ while (1) { \
81
+ if (*buf == ' ') { \
82
+ break; \
83
+ } else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
84
+ if ((unsigned char)*buf < '\040' || *buf == '\177') { \
85
+ *ret = -1; \
86
+ return NULL; \
87
+ } \
88
+ } \
89
+ ++buf; \
90
+ CHECK_EOF(); \
91
+ } \
92
+ tok = tok_start; \
93
+ toklen = buf - tok_start; \
94
+ } while (0)
95
+
96
+ static const char *token_char_map = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
97
+ "\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
98
+ "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
99
+ "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
100
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
101
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
102
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
103
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
104
+
105
+ static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
106
+ {
107
+ *found = 0;
108
+ #if __SSE4_2__
109
+ if (likely(buf_end - buf >= 16)) {
110
+ __m128i ranges16 = _mm_loadu_si128((const __m128i *)ranges);
111
+
112
+ size_t left = (buf_end - buf) & ~15;
113
+ do {
114
+ __m128i b16 = _mm_loadu_si128((const __m128i *)buf);
115
+ int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
116
+ if (unlikely(r != 16)) {
117
+ buf += r;
118
+ *found = 1;
119
+ break;
120
+ }
121
+ buf += 16;
122
+ left -= 16;
123
+ } while (likely(left != 0));
124
+ }
125
+ #else
126
+ /* suppress unused parameter warning */
127
+ (void)buf_end;
128
+ (void)ranges;
129
+ (void)ranges_size;
130
+ #endif
131
+ return buf;
132
+ }
133
+
134
+ static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret)
135
+ {
136
+ const char *token_start = buf;
137
+
138
+ #ifdef __SSE4_2__
139
+ static const char ALIGNED(16) ranges1[16] = "\0\010" /* allow HT */
140
+ "\012\037" /* allow SP and up to but not including DEL */
141
+ "\177\177"; /* allow chars w. MSB set */
142
+ int found;
143
+ buf = findchar_fast(buf, buf_end, ranges1, 6, &found);
144
+ if (found)
145
+ goto FOUND_CTL;
146
+ #else
147
+ /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
148
+ while (likely(buf_end - buf >= 8)) {
149
+ #define DOIT() \
150
+ do { \
151
+ if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \
152
+ goto NonPrintable; \
153
+ ++buf; \
154
+ } while (0)
155
+ DOIT();
156
+ DOIT();
157
+ DOIT();
158
+ DOIT();
159
+ DOIT();
160
+ DOIT();
161
+ DOIT();
162
+ DOIT();
163
+ #undef DOIT
164
+ continue;
165
+ NonPrintable:
166
+ if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
167
+ goto FOUND_CTL;
168
+ }
169
+ ++buf;
170
+ }
171
+ #endif
172
+ for (;; ++buf) {
173
+ CHECK_EOF();
174
+ if (unlikely(!IS_PRINTABLE_ASCII(*buf))) {
175
+ if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
176
+ goto FOUND_CTL;
177
+ }
178
+ }
179
+ }
180
+ FOUND_CTL:
181
+ if (likely(*buf == '\015')) {
182
+ ++buf;
183
+ EXPECT_CHAR('\012');
184
+ *token_len = buf - 2 - token_start;
185
+ } else if (*buf == '\012') {
186
+ *token_len = buf - token_start;
187
+ ++buf;
188
+ } else {
189
+ *ret = -1;
190
+ return NULL;
191
+ }
192
+ *token = token_start;
193
+
194
+ return buf;
195
+ }
196
+
197
+ static const char *is_complete(const char *buf, const char *buf_end, size_t last_len, int *ret)
198
+ {
199
+ int ret_cnt = 0;
200
+ buf = last_len < 3 ? buf : buf + last_len - 3;
201
+
202
+ while (1) {
203
+ CHECK_EOF();
204
+ if (*buf == '\015') {
205
+ ++buf;
206
+ CHECK_EOF();
207
+ EXPECT_CHAR('\012');
208
+ ++ret_cnt;
209
+ } else if (*buf == '\012') {
210
+ ++buf;
211
+ ++ret_cnt;
212
+ } else {
213
+ ++buf;
214
+ ret_cnt = 0;
215
+ }
216
+ if (ret_cnt == 2) {
217
+ return buf;
218
+ }
219
+ }
220
+
221
+ *ret = -2;
222
+ return NULL;
223
+ }
224
+
225
+ #define PARSE_INT(valp_, mul_) \
226
+ if (*buf < '0' || '9' < *buf) { \
227
+ buf++; \
228
+ *ret = -1; \
229
+ return NULL; \
230
+ } \
231
+ *(valp_) = (mul_) * (*buf++ - '0');
232
+
233
+ #define PARSE_INT_3(valp_) \
234
+ do { \
235
+ int res_ = 0; \
236
+ PARSE_INT(&res_, 100) \
237
+ *valp_ = res_; \
238
+ PARSE_INT(&res_, 10) \
239
+ *valp_ += res_; \
240
+ PARSE_INT(&res_, 1) \
241
+ *valp_ += res_; \
242
+ } while (0)
243
+
244
+ /* returned pointer is always within [buf, buf_end), or null */
245
+ static const char *parse_token(const char *buf, const char *buf_end, const char **token, size_t *token_len, char next_char,
246
+ int *ret)
247
+ {
248
+ /* We use pcmpestri to detect non-token characters. This instruction can take no more than eight character ranges (8*2*8=128
249
+ * bits that is the size of a SSE register). Due to this restriction, characters `|` and `~` are handled in the slow loop. */
250
+ static const char ALIGNED(16) ranges[] = "\x00 " /* control chars and up to SP */
251
+ "\"\"" /* 0x22 */
252
+ "()" /* 0x28,0x29 */
253
+ ",," /* 0x2c */
254
+ "//" /* 0x2f */
255
+ ":@" /* 0x3a-0x40 */
256
+ "[]" /* 0x5b-0x5d */
257
+ "{\xff"; /* 0x7b-0xff */
258
+ const char *buf_start = buf;
259
+ int found;
260
+ buf = findchar_fast(buf, buf_end, ranges, sizeof(ranges) - 1, &found);
261
+ if (!found) {
262
+ CHECK_EOF();
263
+ }
264
+ while (1) {
265
+ if (*buf == next_char) {
266
+ break;
267
+ } else if (!token_char_map[(unsigned char)*buf]) {
268
+ *ret = -1;
269
+ return NULL;
270
+ }
271
+ ++buf;
272
+ CHECK_EOF();
273
+ }
274
+ *token = buf_start;
275
+ *token_len = buf - buf_start;
276
+ return buf;
277
+ }
278
+
279
+ /* returned pointer is always within [buf, buf_end), or null */
280
+ static const char *parse_http_version(const char *buf, const char *buf_end, int *minor_version, int *ret)
281
+ {
282
+ /* we want at least [HTTP/1.<two chars>] to try to parse */
283
+ if (buf_end - buf < 9) {
284
+ *ret = -2;
285
+ return NULL;
286
+ }
287
+ EXPECT_CHAR_NO_CHECK('H');
288
+ EXPECT_CHAR_NO_CHECK('T');
289
+ EXPECT_CHAR_NO_CHECK('T');
290
+ EXPECT_CHAR_NO_CHECK('P');
291
+ EXPECT_CHAR_NO_CHECK('/');
292
+ EXPECT_CHAR_NO_CHECK('1');
293
+ EXPECT_CHAR_NO_CHECK('.');
294
+ PARSE_INT(minor_version, 1);
295
+ return buf;
296
+ }
297
+
298
+ static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers,
299
+ size_t max_headers, int *ret)
300
+ {
301
+ for (;; ++*num_headers) {
302
+ CHECK_EOF();
303
+ if (*buf == '\015') {
304
+ ++buf;
305
+ EXPECT_CHAR('\012');
306
+ break;
307
+ } else if (*buf == '\012') {
308
+ ++buf;
309
+ break;
310
+ }
311
+ if (*num_headers == max_headers) {
312
+ *ret = -1;
313
+ return NULL;
314
+ }
315
+ if (!(*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
316
+ /* parsing name, but do not discard SP before colon, see
317
+ * http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
318
+ if ((buf = parse_token(buf, buf_end, &headers[*num_headers].name, &headers[*num_headers].name_len, ':', ret)) == NULL) {
319
+ return NULL;
320
+ }
321
+ if (headers[*num_headers].name_len == 0) {
322
+ *ret = -1;
323
+ return NULL;
324
+ }
325
+ ++buf;
326
+ for (;; ++buf) {
327
+ CHECK_EOF();
328
+ if (!(*buf == ' ' || *buf == '\t')) {
329
+ break;
330
+ }
331
+ }
332
+ } else {
333
+ headers[*num_headers].name = NULL;
334
+ headers[*num_headers].name_len = 0;
335
+ }
336
+ const char *value;
337
+ size_t value_len;
338
+ if ((buf = get_token_to_eol(buf, buf_end, &value, &value_len, ret)) == NULL) {
339
+ return NULL;
340
+ }
341
+ /* remove trailing SPs and HTABs */
342
+ const char *value_end = value + value_len;
343
+ for (; value_end != value; --value_end) {
344
+ const char c = *(value_end - 1);
345
+ if (!(c == ' ' || c == '\t')) {
346
+ break;
347
+ }
348
+ }
349
+ headers[*num_headers].value = value;
350
+ headers[*num_headers].value_len = value_end - value;
351
+ }
352
+ return buf;
353
+ }
354
+
355
+ static const char *parse_request(const char *buf, const char *buf_end, const char **method, size_t *method_len, const char **path,
356
+ size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers,
357
+ size_t max_headers, int *ret)
358
+ {
359
+ /* skip first empty line (some clients add CRLF after POST content) */
360
+ CHECK_EOF();
361
+ if (*buf == '\015') {
362
+ ++buf;
363
+ EXPECT_CHAR('\012');
364
+ } else if (*buf == '\012') {
365
+ ++buf;
366
+ }
367
+
368
+ /* parse request line */
369
+ if ((buf = parse_token(buf, buf_end, method, method_len, ' ', ret)) == NULL) {
370
+ return NULL;
371
+ }
372
+ do {
373
+ ++buf;
374
+ CHECK_EOF();
375
+ } while (*buf == ' ');
376
+ ADVANCE_TOKEN(*path, *path_len);
377
+ do {
378
+ ++buf;
379
+ CHECK_EOF();
380
+ } while (*buf == ' ');
381
+ if (*method_len == 0 || *path_len == 0) {
382
+ *ret = -1;
383
+ return NULL;
384
+ }
385
+ if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
386
+ return NULL;
387
+ }
388
+ if (*buf == '\015') {
389
+ ++buf;
390
+ EXPECT_CHAR('\012');
391
+ } else if (*buf == '\012') {
392
+ ++buf;
393
+ } else {
394
+ *ret = -1;
395
+ return NULL;
396
+ }
397
+
398
+ return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
399
+ }
400
+
401
+ int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path,
402
+ size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
403
+ {
404
+ const char *buf = buf_start, *buf_end = buf_start + len;
405
+ size_t max_headers = *num_headers;
406
+ int r;
407
+
408
+ *method = NULL;
409
+ *method_len = 0;
410
+ *path = NULL;
411
+ *path_len = 0;
412
+ *minor_version = -1;
413
+ *num_headers = 0;
414
+
415
+ /* if last_len != 0, check if the request is complete (a fast countermeasure
416
+ againt slowloris */
417
+ if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
418
+ return r;
419
+ }
420
+
421
+ if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, minor_version, headers, num_headers, max_headers,
422
+ &r)) == NULL) {
423
+ return r;
424
+ }
425
+
426
+ return (int)(buf - buf_start);
427
+ }
428
+
429
+ static const char *parse_response(const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg,
430
+ size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
431
+ {
432
+ /* parse "HTTP/1.x" */
433
+ if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
434
+ return NULL;
435
+ }
436
+ /* skip space */
437
+ if (*buf != ' ') {
438
+ *ret = -1;
439
+ return NULL;
440
+ }
441
+ do {
442
+ ++buf;
443
+ CHECK_EOF();
444
+ } while (*buf == ' ');
445
+ /* parse status code, we want at least [:digit:][:digit:][:digit:]<other char> to try to parse */
446
+ if (buf_end - buf < 4) {
447
+ *ret = -2;
448
+ return NULL;
449
+ }
450
+ PARSE_INT_3(status);
451
+
452
+ /* get message including preceding space */
453
+ if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
454
+ return NULL;
455
+ }
456
+ if (*msg_len == 0) {
457
+ /* ok */
458
+ } else if (**msg == ' ') {
459
+ /* Remove preceding space. Successful return from `get_token_to_eol` guarantees that we would hit something other than SP
460
+ * before running past the end of the given buffer. */
461
+ do {
462
+ ++*msg;
463
+ --*msg_len;
464
+ } while (**msg == ' ');
465
+ } else {
466
+ /* garbage found after status code */
467
+ *ret = -1;
468
+ return NULL;
469
+ }
470
+
471
+ return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
472
+ }
473
+
474
+ int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
475
+ struct phr_header *headers, size_t *num_headers, size_t last_len)
476
+ {
477
+ const char *buf = buf_start, *buf_end = buf + len;
478
+ size_t max_headers = *num_headers;
479
+ int r;
480
+
481
+ *minor_version = -1;
482
+ *status = 0;
483
+ *msg = NULL;
484
+ *msg_len = 0;
485
+ *num_headers = 0;
486
+
487
+ /* if last_len != 0, check if the response is complete (a fast countermeasure
488
+ against slowloris */
489
+ if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
490
+ return r;
491
+ }
492
+
493
+ if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, headers, num_headers, max_headers, &r)) == NULL) {
494
+ return r;
495
+ }
496
+
497
+ return (int)(buf - buf_start);
498
+ }
499
+
500
+ int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len)
501
+ {
502
+ const char *buf = buf_start, *buf_end = buf + len;
503
+ size_t max_headers = *num_headers;
504
+ int r;
505
+
506
+ *num_headers = 0;
507
+
508
+ /* if last_len != 0, check if the response is complete (a fast countermeasure
509
+ against slowloris */
510
+ if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
511
+ return r;
512
+ }
513
+
514
+ if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
515
+ return r;
516
+ }
517
+
518
+ return (int)(buf - buf_start);
519
+ }
520
+
521
+ enum {
522
+ CHUNKED_IN_CHUNK_SIZE,
523
+ CHUNKED_IN_CHUNK_EXT,
524
+ CHUNKED_IN_CHUNK_HEADER_EXPECT_LF,
525
+ CHUNKED_IN_CHUNK_DATA,
526
+ CHUNKED_IN_CHUNK_DATA_EXPECT_CR,
527
+ CHUNKED_IN_CHUNK_DATA_EXPECT_LF,
528
+ CHUNKED_IN_TRAILERS_LINE_HEAD,
529
+ CHUNKED_IN_TRAILERS_LINE_MIDDLE
530
+ };
531
+
532
+ static int decode_hex(int ch)
533
+ {
534
+ if ('0' <= ch && ch <= '9') {
535
+ return ch - '0';
536
+ } else if ('A' <= ch && ch <= 'F') {
537
+ return ch - 'A' + 0xa;
538
+ } else if ('a' <= ch && ch <= 'f') {
539
+ return ch - 'a' + 0xa;
540
+ } else {
541
+ return -1;
542
+ }
543
+ }
544
+
545
+ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *_bufsz)
546
+ {
547
+ size_t dst = 0, src = 0, bufsz = *_bufsz;
548
+ ssize_t ret = -2; /* incomplete */
549
+
550
+ decoder->_total_read += bufsz;
551
+
552
+ while (1) {
553
+ switch (decoder->_state) {
554
+ case CHUNKED_IN_CHUNK_SIZE:
555
+ for (;; ++src) {
556
+ int v;
557
+ if (src == bufsz)
558
+ goto Exit;
559
+ if ((v = decode_hex(buf[src])) == -1) {
560
+ if (decoder->_hex_count == 0) {
561
+ ret = -1;
562
+ goto Exit;
563
+ }
564
+ /* the only characters that may appear after the chunk size are BWS, semicolon, or CRLF */
565
+ switch (buf[src]) {
566
+ case ' ':
567
+ case '\011':
568
+ case ';':
569
+ case '\012':
570
+ case '\015':
571
+ break;
572
+ default:
573
+ ret = -1;
574
+ goto Exit;
575
+ }
576
+ break;
577
+ }
578
+ if (decoder->_hex_count == sizeof(size_t) * 2) {
579
+ ret = -1;
580
+ goto Exit;
581
+ }
582
+ decoder->bytes_left_in_chunk = decoder->bytes_left_in_chunk * 16 + v;
583
+ ++decoder->_hex_count;
584
+ }
585
+ decoder->_hex_count = 0;
586
+ decoder->_state = CHUNKED_IN_CHUNK_EXT;
587
+ /* fallthru */
588
+ case CHUNKED_IN_CHUNK_EXT:
589
+ /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */
590
+ for (;; ++src) {
591
+ if (src == bufsz)
592
+ goto Exit;
593
+ if (buf[src] == '\015') {
594
+ break;
595
+ } else if (buf[src] == '\012') {
596
+ ret = -1;
597
+ goto Exit;
598
+ }
599
+ }
600
+ ++src;
601
+ decoder->_state = CHUNKED_IN_CHUNK_HEADER_EXPECT_LF;
602
+ /* fallthru */
603
+ case CHUNKED_IN_CHUNK_HEADER_EXPECT_LF:
604
+ if (src == bufsz)
605
+ goto Exit;
606
+ if (buf[src] != '\012') {
607
+ ret = -1;
608
+ goto Exit;
609
+ }
610
+ ++src;
611
+ if (decoder->bytes_left_in_chunk == 0) {
612
+ if (decoder->consume_trailer) {
613
+ decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
614
+ break;
615
+ } else {
616
+ goto Complete;
617
+ }
618
+ }
619
+ decoder->_state = CHUNKED_IN_CHUNK_DATA;
620
+ /* fallthru */
621
+ case CHUNKED_IN_CHUNK_DATA: {
622
+ size_t avail = bufsz - src;
623
+ if (avail < decoder->bytes_left_in_chunk) {
624
+ if (dst != src)
625
+ memmove(buf + dst, buf + src, avail);
626
+ src += avail;
627
+ dst += avail;
628
+ decoder->bytes_left_in_chunk -= avail;
629
+ goto Exit;
630
+ }
631
+ if (dst != src)
632
+ memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk);
633
+ src += decoder->bytes_left_in_chunk;
634
+ dst += decoder->bytes_left_in_chunk;
635
+ decoder->bytes_left_in_chunk = 0;
636
+ decoder->_state = CHUNKED_IN_CHUNK_DATA_EXPECT_CR;
637
+ }
638
+ /* fallthru */
639
+ case CHUNKED_IN_CHUNK_DATA_EXPECT_CR:
640
+ if (src == bufsz)
641
+ goto Exit;
642
+ if (buf[src] != '\015') {
643
+ ret = -1;
644
+ goto Exit;
645
+ }
646
+ ++src;
647
+ decoder->_state = CHUNKED_IN_CHUNK_DATA_EXPECT_LF;
648
+ /* fallthru */
649
+ case CHUNKED_IN_CHUNK_DATA_EXPECT_LF:
650
+ if (src == bufsz)
651
+ goto Exit;
652
+ if (buf[src] != '\012') {
653
+ ret = -1;
654
+ goto Exit;
655
+ }
656
+ ++src;
657
+ decoder->_state = CHUNKED_IN_CHUNK_SIZE;
658
+ break;
659
+ case CHUNKED_IN_TRAILERS_LINE_HEAD:
660
+ for (;; ++src) {
661
+ if (src == bufsz)
662
+ goto Exit;
663
+ if (buf[src] != '\015')
664
+ break;
665
+ }
666
+ if (buf[src++] == '\012')
667
+ goto Complete;
668
+ decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE;
669
+ /* fallthru */
670
+ case CHUNKED_IN_TRAILERS_LINE_MIDDLE:
671
+ for (;; ++src) {
672
+ if (src == bufsz)
673
+ goto Exit;
674
+ if (buf[src] == '\012')
675
+ break;
676
+ }
677
+ ++src;
678
+ decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD;
679
+ break;
680
+ default:
681
+ assert(!"decoder is corrupt");
682
+ }
683
+ }
684
+
685
+ Complete:
686
+ ret = bufsz - src;
687
+ Exit:
688
+ if (dst != src)
689
+ memmove(buf + dst, buf + src, bufsz - src);
690
+ *_bufsz = dst;
691
+ /* if incomplete but the overhead of the chunked encoding is >=100KB and >80%, signal an error */
692
+ if (ret == -2) {
693
+ decoder->_total_overhead += bufsz - dst;
694
+ if (decoder->_total_overhead >= 100 * 1024 && decoder->_total_read - decoder->_total_overhead < decoder->_total_read / 4)
695
+ ret = -1;
696
+ }
697
+ return ret;
698
+ }
699
+
700
+ int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder)
701
+ {
702
+ return decoder->_state == CHUNKED_IN_CHUNK_DATA;
703
+ }
704
+
705
+ #undef CHECK_EOF
706
+ #undef EXPECT_CHAR
707
+ #undef ADVANCE_TOKEN
@@ -0,0 +1,90 @@
1
+ /*
2
+ * Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
3
+ * Shigeo Mitsunari
4
+ *
5
+ * The software is licensed under either the MIT License (below) or the Perl
6
+ * license.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to
10
+ * deal in the Software without restriction, including without limitation the
11
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
+ * sell copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in
16
+ * all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
+ * IN THE SOFTWARE.
25
+ */
26
+
27
+ #ifndef picohttpparser_h
28
+ #define picohttpparser_h
29
+
30
+ #include <stdint.h>
31
+ #include <sys/types.h>
32
+
33
+ #ifdef _MSC_VER
34
+ #define ssize_t intptr_t
35
+ #endif
36
+
37
+ #ifdef __cplusplus
38
+ extern "C" {
39
+ #endif
40
+
41
+ /* contains name and value of a header (name == NULL if is a continuing line
42
+ * of a multiline header */
43
+ struct phr_header {
44
+ const char *name;
45
+ size_t name_len;
46
+ const char *value;
47
+ size_t value_len;
48
+ };
49
+
50
+ /* returns number of bytes consumed if successful, -2 if request is partial,
51
+ * -1 if failed */
52
+ int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
53
+ int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
54
+
55
+ /* ditto */
56
+ int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
57
+ struct phr_header *headers, size_t *num_headers, size_t last_len);
58
+
59
+ /* ditto */
60
+ int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
61
+
62
+ /* should be zero-filled before start */
63
+ struct phr_chunked_decoder {
64
+ size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
65
+ char consume_trailer; /* if trailing headers should be consumed */
66
+ char _hex_count;
67
+ char _state;
68
+ uint64_t _total_read;
69
+ uint64_t _total_overhead;
70
+ };
71
+
72
+ /* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
73
+ * encoding headers. When the function returns without an error, bufsz is
74
+ * updated to the length of the decoded data available. Applications should
75
+ * repeatedly call the function while it returns -2 (incomplete) every time
76
+ * supplying newly arrived data. If the end of the chunked-encoded data is
77
+ * found, the function returns a non-negative number indicating the number of
78
+ * octets left undecoded, that starts from the offset returned by `*bufsz`.
79
+ * Returns -1 on error.
80
+ */
81
+ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz);
82
+
83
+ /* returns if the chunked decoder is in middle of chunked data */
84
+ int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
85
+
86
+ #ifdef __cplusplus
87
+ }
88
+ #endif
89
+
90
+ #endif
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Picohttp
4
+ VERSION = "0.1.0"
5
+ end
data/lib/picohttp.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "picohttp/version"
4
+ require_relative "picohttp/picohttp"
5
+
6
+ module Picohttp
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: picohttp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Hawthorn
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Wraps picohttpparser to parse HTTP request strings into Rack-compatible
13
+ hashes
14
+ email:
15
+ - john@hawthorn.email
16
+ executables: []
17
+ extensions:
18
+ - ext/picohttp/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - ext/picohttp/extconf.rb
26
+ - ext/picohttp/picohttp.c
27
+ - ext/picohttp/picohttp.h
28
+ - ext/picohttp/picohttpparser.c
29
+ - ext/picohttp/picohttpparser.h
30
+ - lib/picohttp.rb
31
+ - lib/picohttp/version.rb
32
+ homepage: https://github.com/jhawthorn/picohttp
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/jhawthorn/picohttp
37
+ source_code_uri: https://github.com/jhawthorn/picohttp
38
+ changelog_uri: https://github.com/jhawthorn/picohttp/blob/main/CHANGELOG.md
39
+ bug_tracker_uri: https://github.com/jhawthorn/picohttp/issues
40
+ documentation_uri: https://github.com/jhawthorn/picohttp#readme
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.2.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.6.9
56
+ specification_version: 4
57
+ summary: Fast HTTP request parser using picohttpparser
58
+ test_files: []