nyara 0.0.1.pre

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.
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,156 @@
1
+ /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
2
+ *
3
+ * Additional changes are licensed under the same terms as NGINX and
4
+ * copyright Joyent, Inc. and other Node contributors. All rights reserved.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to
8
+ * deal in the Software without restriction, including without limitation the
9
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
+ * sell copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
+ * IN THE SOFTWARE.
23
+ */
24
+
25
+ /* Dump what the parser finds to stdout as it happen */
26
+
27
+ #include "http_parser.h"
28
+ #include <stdio.h>
29
+ #include <stdlib.h>
30
+ #include <string.h>
31
+
32
+ int on_message_begin(http_parser* _) {
33
+ (void)_;
34
+ printf("\n***MESSAGE BEGIN***\n\n");
35
+ return 0;
36
+ }
37
+
38
+ int on_headers_complete(http_parser* _) {
39
+ (void)_;
40
+ printf("\n***HEADERS COMPLETE***\n\n");
41
+ return 0;
42
+ }
43
+
44
+ int on_message_complete(http_parser* _) {
45
+ (void)_;
46
+ printf("\n***MESSAGE COMPLETE***\n\n");
47
+ return 0;
48
+ }
49
+
50
+ int on_url(http_parser* _, const char* at, size_t length) {
51
+ (void)_;
52
+ printf("Url: %.*s\n", (int)length, at);
53
+ return 0;
54
+ }
55
+
56
+ int on_header_field(http_parser* _, const char* at, size_t length) {
57
+ (void)_;
58
+ printf("Header field: %.*s\n", (int)length, at);
59
+ return 0;
60
+ }
61
+
62
+ int on_header_value(http_parser* _, const char* at, size_t length) {
63
+ (void)_;
64
+ printf("Header value: %.*s\n", (int)length, at);
65
+ return 0;
66
+ }
67
+
68
+ int on_body(http_parser* _, const char* at, size_t length) {
69
+ (void)_;
70
+ printf("Body: %.*s\n", (int)length, at);
71
+ return 0;
72
+ }
73
+
74
+ void usage(const char* name) {
75
+ fprintf(stderr,
76
+ "Usage: %s $type $filename\n"
77
+ " type: -x, where x is one of {r,b,q}\n"
78
+ " parses file as a Response, reQuest, or Both\n",
79
+ name);
80
+ exit(EXIT_FAILURE);
81
+ }
82
+
83
+ int main(int argc, char* argv[]) {
84
+ enum http_parser_type file_type;
85
+
86
+ if (argc != 3) {
87
+ usage(argv[0]);
88
+ }
89
+
90
+ char* type = argv[1];
91
+ if (type[0] != '-') {
92
+ usage(argv[0]);
93
+ }
94
+
95
+ switch (type[1]) {
96
+ /* in the case of "-", type[1] will be NUL */
97
+ case 'r':
98
+ file_type = HTTP_RESPONSE;
99
+ break;
100
+ case 'q':
101
+ file_type = HTTP_REQUEST;
102
+ break;
103
+ case 'b':
104
+ file_type = HTTP_BOTH;
105
+ break;
106
+ default:
107
+ usage(argv[0]);
108
+ }
109
+
110
+ char* filename = argv[2];
111
+ FILE* file = fopen(filename, "r");
112
+ if (file == NULL) {
113
+ perror("fopen");
114
+ return EXIT_FAILURE;
115
+ }
116
+
117
+ fseek(file, 0, SEEK_END);
118
+ long file_length = ftell(file);
119
+ if (file_length == -1) {
120
+ perror("ftell");
121
+ return EXIT_FAILURE;
122
+ }
123
+ fseek(file, 0, SEEK_SET);
124
+
125
+ char* data = malloc(file_length);
126
+ if (fread(data, 1, file_length, file) != (size_t)file_length) {
127
+ fprintf(stderr, "couldn't read entire file\n");
128
+ free(data);
129
+ return EXIT_FAILURE;
130
+ }
131
+
132
+ http_parser_settings settings;
133
+ memset(&settings, 0, sizeof(settings));
134
+ settings.on_message_begin = on_message_begin;
135
+ settings.on_url = on_url;
136
+ settings.on_header_field = on_header_field;
137
+ settings.on_header_value = on_header_value;
138
+ settings.on_headers_complete = on_headers_complete;
139
+ settings.on_body = on_body;
140
+ settings.on_message_complete = on_message_complete;
141
+
142
+ http_parser parser;
143
+ http_parser_init(&parser, file_type);
144
+ size_t nparsed = http_parser_execute(&parser, &settings, data, file_length);
145
+ free(data);
146
+
147
+ if (nparsed != (size_t)file_length) {
148
+ fprintf(stderr,
149
+ "Error: %s (%s)\n",
150
+ http_errno_description(HTTP_PARSER_ERRNO(&parser)),
151
+ http_errno_name(HTTP_PARSER_ERRNO(&parser)));
152
+ return EXIT_FAILURE;
153
+ }
154
+
155
+ return EXIT_SUCCESS;
156
+ }
@@ -0,0 +1,44 @@
1
+ #include "http_parser.h"
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+
5
+ void
6
+ dump_url (const char *url, const struct http_parser_url *u)
7
+ {
8
+ unsigned int i;
9
+
10
+ printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
11
+ for (i = 0; i < UF_MAX; i++) {
12
+ if ((u->field_set & (1 << i)) == 0) {
13
+ printf("\tfield_data[%u]: unset\n", i);
14
+ continue;
15
+ }
16
+
17
+ printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n",
18
+ i,
19
+ u->field_data[i].off,
20
+ u->field_data[i].len,
21
+ u->field_data[i].len,
22
+ url + u->field_data[i].off);
23
+ }
24
+ }
25
+
26
+ int main(int argc, char ** argv) {
27
+ if (argc != 3) {
28
+ printf("Syntax : %s connect|get url\n", argv[0]);
29
+ return 1;
30
+ }
31
+ struct http_parser_url u;
32
+ int len = strlen(argv[2]);
33
+ int connect = strcmp("connect", argv[1]) == 0 ? 1 : 0;
34
+ printf("Parsing %s, connect %d\n", argv[2], connect);
35
+
36
+ int result = http_parser_parse_url(argv[2], len, connect, &u);
37
+ if (result != 0) {
38
+ printf("Parse error : %d\n", result);
39
+ return result;
40
+ }
41
+ printf("Parse ok, result : \n");
42
+ dump_url(argv[2], &u);
43
+ return 0;
44
+ }