|
| 1 | +# ext-picohttpparser |
| 2 | + |
| 3 | +A PHP port of the fast HTTP parser used in the [H2O project](https://github.com/h2o/h2o). |
| 4 | + |
| 5 | +## Rationale |
| 6 | + |
| 7 | +The picohttpparser project is a tiny, (near) stateless HTTP parser. The performance gains associated with the library are largely attributable to its zero-copy internals: it is almost entirely reliant on stack-allocated data structures. `ext-picohttpparser` is an attempt to expose the API of the aforedescribed eponymous lightweight, efficient parser to the PHP userspace. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- PHP 8.1 or newer |
| 12 | +- [picohttpparser](https://github.com/h2o/picohttpparser) |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +It is important to have all the aforelisted requirements at the ready before attempting to install `ext-picohttpparser`. The directives in the snippet to follow should allow you to build the extension's shared object file (`picohttpparser.so`). |
| 17 | + |
| 18 | +```sh |
| 19 | +$ git clone https://github.com/h2o/picohttpparser.git <picohttp-dir> |
| 20 | +$ git clone https://github.com/ringphp/php-picohttpparser.git <dir> |
| 21 | +$ cd <dir> |
| 22 | +$ phpize |
| 23 | +$ ./configure --with-picohttpparser=<picohttp-dir> |
| 24 | +$ make && sudo make install |
| 25 | +``` |
| 26 | + |
| 27 | +After successfully building the shared object, proceed to operationalize the extension by adding the line `extension=picohttpparser` to your `php.ini` file. If you prefer to perform the said operationalization via command line interface, the following should suffice. |
| 28 | + |
| 29 | +```sh |
| 30 | +$ printf "\nextension=picohttpparser\n" >> "$(php-config --ini-path)/php.ini" |
| 31 | +``` |
| 32 | + |
| 33 | +## API Synopsis |
| 34 | + |
| 35 | +```php |
| 36 | +/* core functions */ |
| 37 | +picohttp_parse_request(string $request, int $header_limit = 100): array |
| 38 | +picohttp_parse_response(string $response, int $header_limit = 100): array |
| 39 | +``` |
| 40 | + |
| 41 | +- [picohttp_parse_request](#picohttp_parse_request) |
| 42 | +- [picohttp_parse_response](#picohttp_parse_response) |
| 43 | + |
| 44 | +### `parse_http_request` |
| 45 | + |
| 46 | +```php |
| 47 | +picohttp_parse_request(string $request, int $header_limit = 100): array |
| 48 | +``` |
| 49 | + |
| 50 | +Parses an HTTP request. |
| 51 | + |
| 52 | +**Parameter(s)** |
| 53 | + |
| 54 | +- **request** (string) - The HTTP request to parse. |
| 55 | +- **header_limit** (int) - The number of headers to parse. |
| 56 | + > The default limit is `100`. |
| 57 | +
|
| 58 | +**Return value(s)** |
| 59 | + |
| 60 | +The parser will throw an exception in the event that an invalid HTTP request is encountered and will output a hashtable with the contents enumerated below otherwise. |
| 61 | + |
| 62 | +- **body** (string) - The request body. |
| 63 | +- **headers** (iterable) - An associative array containing request headers. |
| 64 | +- **method** (string) - The request method. |
| 65 | +- **path** (string) - The request path. |
| 66 | + |
| 67 | +```php |
| 68 | +$request = <<<EOF |
| 69 | +GET / HTTP/1.1 |
| 70 | +host: localhost:8080 |
| 71 | +accept: */* |
| 72 | + |
| 73 | + |
| 74 | +EOF; |
| 75 | + |
| 76 | +var_dump( |
| 77 | + \picohttp_parse_request($request), |
| 78 | +); |
| 79 | +``` |
| 80 | + |
| 81 | +The example above will produce output similar to that in the snippet to follow. |
| 82 | + |
| 83 | +``` |
| 84 | +array(4) { |
| 85 | + ["path"]=> |
| 86 | + string(1) "/" |
| 87 | + ["method"]=> |
| 88 | + string(3) "GET" |
| 89 | + ["body"]=> |
| 90 | + string(0) "" |
| 91 | + ["headers"]=> |
| 92 | + array(2) { |
| 93 | + ["host"]=> |
| 94 | + string(14) "localhost:8080" |
| 95 | + ["accept"]=> |
| 96 | + string(3) "*/*" |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### `picohttp_parse_response` |
| 102 | + |
| 103 | +```php |
| 104 | +picohttp_parse_response(string $response, int $header_limit = 100): array |
| 105 | +``` |
| 106 | + |
| 107 | +Parses an HTTP response. |
| 108 | + |
| 109 | +**Parameter(s)** |
| 110 | + |
| 111 | +- **response** (string) - The HTTP response to parse. |
| 112 | +- **header_limit** (int) - The number of headers to parse. |
| 113 | + > The default limit is `100`. |
| 114 | +
|
| 115 | +**Return value(s)** |
| 116 | + |
| 117 | +The parser will throw an exception in the event that an invalid HTTP response is encountered and will output a hashtable with the contents enumerated below otherwise. |
| 118 | + |
| 119 | +- **body** (string) - The response body. |
| 120 | +- **headers** (iterable) - An associative array containing response headers. |
| 121 | +- **status** (int) - The response status code. |
| 122 | +- **reason** (string) - The response reason phrase. |
| 123 | + |
| 124 | +```php |
| 125 | +$response = <<<EOF |
| 126 | +HTTP/1.1 200 OK |
| 127 | +server: ringphp |
| 128 | +content-type: application/json; charset=utf-8 |
| 129 | +content-length: 37 |
| 130 | + |
| 131 | +{ |
| 132 | + "foo": "foo", |
| 133 | + "bar": "bar" |
| 134 | +} |
| 135 | +EOF; |
| 136 | + |
| 137 | +var_dump( |
| 138 | + \picohttp_parse_response($response), |
| 139 | +); |
| 140 | +``` |
| 141 | + |
| 142 | +The example above will produce output similar to that in the snippet to follow. |
| 143 | + |
| 144 | +``` |
| 145 | +array(4) { |
| 146 | + ["reason"]=> |
| 147 | + string(2) "OK" |
| 148 | + ["status"]=> |
| 149 | + int(200) |
| 150 | + ["body"]=> |
| 151 | + string(37) "{ |
| 152 | + "foo": "foo", |
| 153 | + "bar": "bar" |
| 154 | +}" |
| 155 | + ["headers"]=> |
| 156 | + array(3) { |
| 157 | + ["server"]=> |
| 158 | + string(7) "ringphp" |
| 159 | + ["content-type"]=> |
| 160 | + string(16) "application/json; charset=utf-8" |
| 161 | + ["content-length"]=> |
| 162 | + string(2) "37" |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
0 commit comments