diff options
Diffstat (limited to 'src/examples/mhd2spdy_structures.c')
-rw-r--r-- | src/examples/mhd2spdy_structures.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/examples/mhd2spdy_structures.c b/src/examples/mhd2spdy_structures.c new file mode 100644 index 00000000..a3486e3a --- /dev/null +++ b/src/examples/mhd2spdy_structures.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /* | ||
2 | Copyright (C) 2013 Andrey Uzunov | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 3 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | /** | ||
19 | * @file structures.h | ||
20 | * @author Andrey Uzunov | ||
21 | */ | ||
22 | |||
23 | #include "mhd2spdy_structures.h" | ||
24 | |||
25 | |||
26 | void | ||
27 | free_uri(struct URI * uri) | ||
28 | { | ||
29 | if(NULL != uri) | ||
30 | { | ||
31 | free(uri->full_uri); | ||
32 | free(uri->scheme); | ||
33 | free(uri->host_and_port); | ||
34 | //free(uri->host_and_port_for_connecting); | ||
35 | free(uri->host); | ||
36 | free(uri->path); | ||
37 | free(uri->path_and_more); | ||
38 | free(uri->query); | ||
39 | free(uri->fragment); | ||
40 | uri->port = 0; | ||
41 | free(uri); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | int | ||
46 | init_parse_uri(regex_t * preg) | ||
47 | { | ||
48 | // RFC 2396 | ||
49 | // ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? | ||
50 | /* | ||
51 | scheme = $2 | ||
52 | authority = $4 | ||
53 | path = $5 | ||
54 | query = $7 | ||
55 | fragment = $9 | ||
56 | */ | ||
57 | |||
58 | return regcomp(preg, "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?", REG_EXTENDED); | ||
59 | } | ||
60 | |||
61 | void | ||
62 | deinit_parse_uri(regex_t * preg) | ||
63 | { | ||
64 | regfree(preg); | ||
65 | } | ||
66 | |||
67 | int | ||
68 | parse_uri(regex_t * preg, char * full_uri, struct URI ** uri) | ||
69 | { | ||
70 | int ret; | ||
71 | char *colon; | ||
72 | long long port; | ||
73 | size_t nmatch = 10; | ||
74 | regmatch_t pmatch[10]; | ||
75 | |||
76 | if (0 != (ret = regexec(preg, full_uri, nmatch, pmatch, 0))) | ||
77 | return ret; | ||
78 | |||
79 | *uri = au_malloc(sizeof(struct URI)); | ||
80 | if(NULL == *uri) | ||
81 | return -200; | ||
82 | |||
83 | (*uri)->full_uri = strdup(full_uri); | ||
84 | |||
85 | asprintf(&((*uri)->scheme), "%.*s",pmatch[2].rm_eo - pmatch[2].rm_so, &full_uri[pmatch[2].rm_so]); | ||
86 | asprintf(&((*uri)->host_and_port), "%.*s",pmatch[4].rm_eo - pmatch[4].rm_so, &full_uri[pmatch[4].rm_so]); | ||
87 | asprintf(&((*uri)->path), "%.*s",pmatch[5].rm_eo - pmatch[5].rm_so, &full_uri[pmatch[5].rm_so]); | ||
88 | asprintf(&((*uri)->path_and_more), "%.*s",pmatch[9].rm_eo - pmatch[5].rm_so, &full_uri[pmatch[5].rm_so]); | ||
89 | asprintf(&((*uri)->query), "%.*s",pmatch[7].rm_eo - pmatch[7].rm_so, &full_uri[pmatch[7].rm_so]); | ||
90 | asprintf(&((*uri)->fragment), "%.*s",pmatch[9].rm_eo - pmatch[9].rm_so, &full_uri[pmatch[9].rm_so]); | ||
91 | |||
92 | colon = strrchr((*uri)->host_and_port, ':'); | ||
93 | if(NULL == colon) | ||
94 | { | ||
95 | (*uri)->host = strdup((*uri)->host_and_port); | ||
96 | /*if(0 == strcasecmp("http", uri->scheme)) | ||
97 | { | ||
98 | uri->port = 80; | ||
99 | asprintf(&(uri->host_and_port_for_connecting), "%s:80", uri->host_and_port); | ||
100 | } | ||
101 | else if(0 == strcasecmp("https", uri->scheme)) | ||
102 | { | ||
103 | uri->port = 443; | ||
104 | asprintf(&(uri->host_and_port_for_connecting), "%s:443", uri->host_and_port); | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | PRINT_INFO("no standard scheme!"); | ||
109 | */(*uri)->port = 0; | ||
110 | /*uri->host_and_port_for_connecting = strdup(uri->host_and_port); | ||
111 | }*/ | ||
112 | return 0; | ||
113 | } | ||
114 | |||
115 | port = atoi(colon + 1); | ||
116 | if(port<1 || port >= 256 * 256) | ||
117 | { | ||
118 | free_uri(*uri); | ||
119 | return -100; | ||
120 | } | ||
121 | (*uri)->port = port; | ||
122 | asprintf(&((*uri)->host), "%.*s", (int)(colon - (*uri)->host_and_port), (*uri)->host_and_port); | ||
123 | |||
124 | return 0; | ||
125 | } | ||
126 | |||
127 | void | ||
128 | free_proxy(struct Proxy *proxy) | ||
129 | { | ||
130 | //PRINT_INFO("free proxy called"); | ||
131 | free(proxy->http_body); | ||
132 | free_uri(proxy->uri); | ||
133 | free(proxy->url); | ||
134 | free(proxy->http_uri); | ||
135 | free(proxy); | ||
136 | } | ||
137 | |||
138 | void *au_malloc(size_t size) | ||
139 | { | ||
140 | void *new_memory; | ||
141 | |||
142 | new_memory = malloc(size); | ||
143 | if(NULL != new_memory) | ||
144 | { | ||
145 | glob_opt.global_memory += size; | ||
146 | memset(new_memory, 0, size); | ||
147 | } | ||
148 | return new_memory; | ||
149 | } | ||