1 /* 2 Copyright 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 mhd2spdy_structures.h 20 * @brief Common functions, macros. 21 * @author Andrey Uzunov 22 */ 23 24 #include "mhd2spdy_structures.h" 25 26 27 void 28 free_uri(struct URI * uri) 29 { 30 if(NULL != uri) 31 { 32 free(uri->full_uri); 33 free(uri->scheme); 34 free(uri->host_and_port); 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 46 int 47 init_parse_uri(regex_t * preg) 48 { 49 // RFC 2396 50 // ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 51 /* 52 scheme = $2 53 authority = $4 54 path = $5 55 query = $7 56 fragment = $9 57 */ 58 59 return regcomp(preg, "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?", REG_EXTENDED); 60 } 61 62 void 63 deinit_parse_uri(regex_t * preg) 64 { 65 regfree(preg); 66 } 67 68 int 69 parse_uri(regex_t * preg, 70 char * full_uri, 71 struct URI ** uri) 72 { 73 int ret; 74 char *colon; 75 long long port; 76 size_t nmatch = 10; 77 regmatch_t pmatch[10]; 78 79 if (0 != (ret = regexec(preg, full_uri, nmatch, pmatch, 0))) 80 return ret; 81 82 *uri = au_malloc(sizeof(struct URI)); 83 if(NULL == *uri) 84 return -200; 85 86 (*uri)->full_uri = strdup(full_uri); 87 88 asprintf(&((*uri)->scheme), "%.*s",pmatch[2].rm_eo - pmatch[2].rm_so, &full_uri[pmatch[2].rm_so]); 89 asprintf(&((*uri)->host_and_port), "%.*s",pmatch[4].rm_eo - pmatch[4].rm_so, &full_uri[pmatch[4].rm_so]); 90 asprintf(&((*uri)->path), "%.*s",pmatch[5].rm_eo - pmatch[5].rm_so, &full_uri[pmatch[5].rm_so]); 91 asprintf(&((*uri)->path_and_more), "%.*s",pmatch[9].rm_eo - pmatch[5].rm_so, &full_uri[pmatch[5].rm_so]); 92 asprintf(&((*uri)->query), "%.*s",pmatch[7].rm_eo - pmatch[7].rm_so, &full_uri[pmatch[7].rm_so]); 93 asprintf(&((*uri)->fragment), "%.*s",pmatch[9].rm_eo - pmatch[9].rm_so, &full_uri[pmatch[9].rm_so]); 94 95 colon = strrchr((*uri)->host_and_port, ':'); 96 if(NULL == colon) 97 { 98 (*uri)->host = strdup((*uri)->host_and_port); 99 (*uri)->port = 0; 100 101 return 0; 102 } 103 104 port = atoi(colon + 1); 105 if(port<1 || port >= 256 * 256) 106 { 107 free_uri(*uri); 108 return -100; 109 } 110 (*uri)->port = port; 111 asprintf(&((*uri)->host), "%.*s", (int)(colon - (*uri)->host_and_port), (*uri)->host_and_port); 112 113 return 0; 114 } 115 116 117 void 118 free_proxy(struct Proxy *proxy) 119 { 120 PRINT_INFO2("free proxy called for '%s'", proxy->url); 121 if(NULL != proxy->http_body && proxy->http_body_size > 0) 122 UPDATE_STAT(glob_stat.spdy_bytes_received_and_dropped, proxy->http_body_size); 123 free(proxy->http_body); 124 free_uri(proxy->uri); 125 free(proxy->url); 126 free(proxy->http_uri); 127 free(proxy); 128 } 129 130 131 void *au_malloc(size_t size) 132 { 133 void *new_memory; 134 135 new_memory = malloc(size); 136 if(NULL != new_memory) 137 { 138 glob_opt.global_memory += size; 139 memset(new_memory, 0, size); 140 } 141 return new_memory; 142 } 143 144 145 bool 146 copy_buffer(const void *src, size_t src_size, void **dst, size_t *dst_size) 147 { 148 if(0 == src_size) 149 return true; 150 151 if(NULL == *dst) 152 *dst = malloc(src_size); 153 else 154 *dst = realloc(*dst, src_size + *dst_size); 155 if(NULL == *dst) 156 return false; 157 158 memcpy(*dst + *dst_size, src, src_size); 159 *dst_size += src_size; 160 161 return true; 162 } 163