nyara 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/example/design.rb +62 -0
  3. data/example/fib.rb +15 -0
  4. data/example/hello.rb +5 -0
  5. data/example/stream.rb +10 -0
  6. data/ext/accept.c +133 -0
  7. data/ext/event.c +89 -0
  8. data/ext/extconf.rb +34 -0
  9. data/ext/hashes.c +130 -0
  10. data/ext/http-parser/AUTHORS +41 -0
  11. data/ext/http-parser/CONTRIBUTIONS +4 -0
  12. data/ext/http-parser/LICENSE-MIT +23 -0
  13. data/ext/http-parser/contrib/parsertrace.c +156 -0
  14. data/ext/http-parser/contrib/url_parser.c +44 -0
  15. data/ext/http-parser/http_parser.c +2175 -0
  16. data/ext/http-parser/http_parser.h +304 -0
  17. data/ext/http-parser/test.c +3425 -0
  18. data/ext/http_parser.c +1 -0
  19. data/ext/inc/epoll.h +60 -0
  20. data/ext/inc/kqueue.h +77 -0
  21. data/ext/inc/status_codes.inc +64 -0
  22. data/ext/inc/str_intern.h +66 -0
  23. data/ext/inc/version.inc +1 -0
  24. data/ext/mime.c +107 -0
  25. data/ext/multipart-parser-c/README.md +18 -0
  26. data/ext/multipart-parser-c/multipart_parser.c +309 -0
  27. data/ext/multipart-parser-c/multipart_parser.h +48 -0
  28. data/ext/multipart_parser.c +1 -0
  29. data/ext/nyara.c +56 -0
  30. data/ext/nyara.h +59 -0
  31. data/ext/request.c +474 -0
  32. data/ext/route.cc +325 -0
  33. data/ext/url_encoded.c +304 -0
  34. data/hello.rb +5 -0
  35. data/lib/nyara/config.rb +64 -0
  36. data/lib/nyara/config_hash.rb +51 -0
  37. data/lib/nyara/controller.rb +336 -0
  38. data/lib/nyara/cookie.rb +31 -0
  39. data/lib/nyara/cpu_counter.rb +65 -0
  40. data/lib/nyara/header_hash.rb +18 -0
  41. data/lib/nyara/mime_types.rb +612 -0
  42. data/lib/nyara/nyara.rb +82 -0
  43. data/lib/nyara/param_hash.rb +5 -0
  44. data/lib/nyara/request.rb +144 -0
  45. data/lib/nyara/route.rb +138 -0
  46. data/lib/nyara/route_entry.rb +43 -0
  47. data/lib/nyara/session.rb +104 -0
  48. data/lib/nyara/view.rb +317 -0
  49. data/lib/nyara.rb +25 -0
  50. data/nyara.gemspec +20 -0
  51. data/rakefile +91 -0
  52. data/readme.md +35 -0
  53. data/spec/ext_mime_match_spec.rb +27 -0
  54. data/spec/ext_parse_accept_value_spec.rb +29 -0
  55. data/spec/ext_parse_spec.rb +138 -0
  56. data/spec/ext_route_spec.rb +70 -0
  57. data/spec/hashes_spec.rb +71 -0
  58. data/spec/path_helper_spec.rb +77 -0
  59. data/spec/request_delegate_spec.rb +67 -0
  60. data/spec/request_spec.rb +56 -0
  61. data/spec/route_entry_spec.rb +12 -0
  62. data/spec/route_spec.rb +84 -0
  63. data/spec/session_spec.rb +66 -0
  64. data/spec/spec_helper.rb +52 -0
  65. data/spec/view_spec.rb +87 -0
  66. data/tools/bench-cookie.rb +22 -0
  67. metadata +111 -0
@@ -0,0 +1,304 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+ #ifndef http_parser_h
22
+ #define http_parser_h
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ #define HTTP_PARSER_VERSION_MAJOR 2
28
+ #define HTTP_PARSER_VERSION_MINOR 1
29
+
30
+ #include <sys/types.h>
31
+ #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
32
+ #include <BaseTsd.h>
33
+ #include <stddef.h>
34
+ typedef __int8 int8_t;
35
+ typedef unsigned __int8 uint8_t;
36
+ typedef __int16 int16_t;
37
+ typedef unsigned __int16 uint16_t;
38
+ typedef __int32 int32_t;
39
+ typedef unsigned __int32 uint32_t;
40
+ typedef __int64 int64_t;
41
+ typedef unsigned __int64 uint64_t;
42
+ #else
43
+ #include <stdint.h>
44
+ #endif
45
+
46
+ /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
47
+ * faster
48
+ */
49
+ #ifndef HTTP_PARSER_STRICT
50
+ # define HTTP_PARSER_STRICT 1
51
+ #endif
52
+
53
+ /* Maximium header size allowed */
54
+ #define HTTP_MAX_HEADER_SIZE (80*1024)
55
+
56
+
57
+ typedef struct http_parser http_parser;
58
+ typedef struct http_parser_settings http_parser_settings;
59
+
60
+
61
+ /* Callbacks should return non-zero to indicate an error. The parser will
62
+ * then halt execution.
63
+ *
64
+ * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
65
+ * returning '1' from on_headers_complete will tell the parser that it
66
+ * should not expect a body. This is used when receiving a response to a
67
+ * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
68
+ * chunked' headers that indicate the presence of a body.
69
+ *
70
+ * http_data_cb does not return data chunks. It will be call arbitrarally
71
+ * many times for each string. E.G. you might get 10 callbacks for "on_url"
72
+ * each providing just a few characters more data.
73
+ */
74
+ typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
75
+ typedef int (*http_cb) (http_parser*);
76
+
77
+
78
+ /* Request Methods */
79
+ #define HTTP_METHOD_MAP(XX) \
80
+ XX(0, DELETE, DELETE) \
81
+ XX(1, GET, GET) \
82
+ XX(2, HEAD, HEAD) \
83
+ XX(3, POST, POST) \
84
+ XX(4, PUT, PUT) \
85
+ /* pathological */ \
86
+ XX(5, CONNECT, CONNECT) \
87
+ XX(6, OPTIONS, OPTIONS) \
88
+ XX(7, TRACE, TRACE) \
89
+ /* webdav */ \
90
+ XX(8, COPY, COPY) \
91
+ XX(9, LOCK, LOCK) \
92
+ XX(10, MKCOL, MKCOL) \
93
+ XX(11, MOVE, MOVE) \
94
+ XX(12, PROPFIND, PROPFIND) \
95
+ XX(13, PROPPATCH, PROPPATCH) \
96
+ XX(14, SEARCH, SEARCH) \
97
+ XX(15, UNLOCK, UNLOCK) \
98
+ /* subversion */ \
99
+ XX(16, REPORT, REPORT) \
100
+ XX(17, MKACTIVITY, MKACTIVITY) \
101
+ XX(18, CHECKOUT, CHECKOUT) \
102
+ XX(19, MERGE, MERGE) \
103
+ /* upnp */ \
104
+ XX(20, MSEARCH, M-SEARCH) \
105
+ XX(21, NOTIFY, NOTIFY) \
106
+ XX(22, SUBSCRIBE, SUBSCRIBE) \
107
+ XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
108
+ /* RFC-5789 */ \
109
+ XX(24, PATCH, PATCH) \
110
+ XX(25, PURGE, PURGE) \
111
+
112
+ enum http_method
113
+ {
114
+ #define XX(num, name, string) HTTP_##name = num,
115
+ HTTP_METHOD_MAP(XX)
116
+ #undef XX
117
+ };
118
+
119
+
120
+ enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
121
+
122
+
123
+ /* Flag values for http_parser.flags field */
124
+ enum flags
125
+ { F_CHUNKED = 1 << 0
126
+ , F_CONNECTION_KEEP_ALIVE = 1 << 1
127
+ , F_CONNECTION_CLOSE = 1 << 2
128
+ , F_TRAILING = 1 << 3
129
+ , F_UPGRADE = 1 << 4
130
+ , F_SKIPBODY = 1 << 5
131
+ };
132
+
133
+
134
+ /* Map for errno-related constants
135
+ *
136
+ * The provided argument should be a macro that takes 2 arguments.
137
+ */
138
+ #define HTTP_ERRNO_MAP(XX) \
139
+ /* No error */ \
140
+ XX(OK, "success") \
141
+ \
142
+ /* Callback-related errors */ \
143
+ XX(CB_message_begin, "the on_message_begin callback failed") \
144
+ XX(CB_status_complete, "the on_status_complete callback failed") \
145
+ XX(CB_url, "the on_url callback failed") \
146
+ XX(CB_header_field, "the on_header_field callback failed") \
147
+ XX(CB_header_value, "the on_header_value callback failed") \
148
+ XX(CB_headers_complete, "the on_headers_complete callback failed") \
149
+ XX(CB_body, "the on_body callback failed") \
150
+ XX(CB_message_complete, "the on_message_complete callback failed") \
151
+ \
152
+ /* Parsing-related errors */ \
153
+ XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
154
+ XX(HEADER_OVERFLOW, \
155
+ "too many header bytes seen; overflow detected") \
156
+ XX(CLOSED_CONNECTION, \
157
+ "data received after completed connection: close message") \
158
+ XX(INVALID_VERSION, "invalid HTTP version") \
159
+ XX(INVALID_STATUS, "invalid HTTP status code") \
160
+ XX(INVALID_METHOD, "invalid HTTP method") \
161
+ XX(INVALID_URL, "invalid URL") \
162
+ XX(INVALID_HOST, "invalid host") \
163
+ XX(INVALID_PORT, "invalid port") \
164
+ XX(INVALID_PATH, "invalid path") \
165
+ XX(INVALID_QUERY_STRING, "invalid query string") \
166
+ XX(INVALID_FRAGMENT, "invalid fragment") \
167
+ XX(LF_EXPECTED, "LF character expected") \
168
+ XX(INVALID_HEADER_TOKEN, "invalid character in header") \
169
+ XX(INVALID_CONTENT_LENGTH, \
170
+ "invalid character in content-length header") \
171
+ XX(INVALID_CHUNK_SIZE, \
172
+ "invalid character in chunk size header") \
173
+ XX(INVALID_CONSTANT, "invalid constant string") \
174
+ XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
175
+ XX(STRICT, "strict mode assertion failed") \
176
+ XX(PAUSED, "parser is paused") \
177
+ XX(UNKNOWN, "an unknown error occurred")
178
+
179
+
180
+ /* Define HPE_* values for each errno value above */
181
+ #define HTTP_ERRNO_GEN(n, s) HPE_##n,
182
+ enum http_errno {
183
+ HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
184
+ };
185
+ #undef HTTP_ERRNO_GEN
186
+
187
+
188
+ /* Get an http_errno value from an http_parser */
189
+ #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
190
+
191
+
192
+ struct http_parser {
193
+ /** PRIVATE **/
194
+ unsigned char type : 2; /* enum http_parser_type */
195
+ unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
196
+ unsigned char state; /* enum state from http_parser.c */
197
+ unsigned char header_state; /* enum header_state from http_parser.c */
198
+ unsigned char index; /* index into current matcher */
199
+
200
+ uint32_t nread; /* # bytes read in various scenarios */
201
+ uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
202
+
203
+ /** READ-ONLY **/
204
+ unsigned short http_major;
205
+ unsigned short http_minor;
206
+ unsigned short status_code; /* responses only */
207
+ unsigned char method; /* requests only */
208
+ unsigned char http_errno : 7;
209
+
210
+ /* 1 = Upgrade header was present and the parser has exited because of that.
211
+ * 0 = No upgrade header present.
212
+ * Should be checked when http_parser_execute() returns in addition to
213
+ * error checking.
214
+ */
215
+ unsigned char upgrade : 1;
216
+
217
+ /** PUBLIC **/
218
+ void *data; /* A pointer to get hook to the "connection" or "socket" object */
219
+ };
220
+
221
+
222
+ struct http_parser_settings {
223
+ http_cb on_message_begin;
224
+ http_data_cb on_url;
225
+ http_cb on_status_complete;
226
+ http_data_cb on_header_field;
227
+ http_data_cb on_header_value;
228
+ http_cb on_headers_complete;
229
+ http_data_cb on_body;
230
+ http_cb on_message_complete;
231
+ };
232
+
233
+
234
+ enum http_parser_url_fields
235
+ { UF_SCHEMA = 0
236
+ , UF_HOST = 1
237
+ , UF_PORT = 2
238
+ , UF_PATH = 3
239
+ , UF_QUERY = 4
240
+ , UF_FRAGMENT = 5
241
+ , UF_USERINFO = 6
242
+ , UF_MAX = 7
243
+ };
244
+
245
+
246
+ /* Result structure for http_parser_parse_url().
247
+ *
248
+ * Callers should index into field_data[] with UF_* values iff field_set
249
+ * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
250
+ * because we probably have padding left over), we convert any port to
251
+ * a uint16_t.
252
+ */
253
+ struct http_parser_url {
254
+ uint16_t field_set; /* Bitmask of (1 << UF_*) values */
255
+ uint16_t port; /* Converted UF_PORT string */
256
+
257
+ struct {
258
+ uint16_t off; /* Offset into buffer in which field starts */
259
+ uint16_t len; /* Length of run in buffer */
260
+ } field_data[UF_MAX];
261
+ };
262
+
263
+
264
+ void http_parser_init(http_parser *parser, enum http_parser_type type);
265
+
266
+
267
+ size_t http_parser_execute(http_parser *parser,
268
+ const http_parser_settings *settings,
269
+ const char *data,
270
+ size_t len);
271
+
272
+
273
+ /* If http_should_keep_alive() in the on_headers_complete or
274
+ * on_message_complete callback returns 0, then this should be
275
+ * the last message on the connection.
276
+ * If you are the server, respond with the "Connection: close" header.
277
+ * If you are the client, close the connection.
278
+ */
279
+ int http_should_keep_alive(const http_parser *parser);
280
+
281
+ /* Returns a string version of the HTTP method. */
282
+ const char *http_method_str(enum http_method m);
283
+
284
+ /* Return a string name of the given error */
285
+ const char *http_errno_name(enum http_errno err);
286
+
287
+ /* Return a string description of the given error */
288
+ const char *http_errno_description(enum http_errno err);
289
+
290
+ /* Parse a URL; return nonzero on failure */
291
+ int http_parser_parse_url(const char *buf, size_t buflen,
292
+ int is_connect,
293
+ struct http_parser_url *u);
294
+
295
+ /* Pause or un-pause the parser; a nonzero value pauses */
296
+ void http_parser_pause(http_parser *parser, int paused);
297
+
298
+ /* Checks if this is the final chunk of the body. */
299
+ int http_body_is_final(const http_parser *parser);
300
+
301
+ #ifdef __cplusplus
302
+ }
303
+ #endif
304
+ #endif