Mercurial > hg > nginx
comparison src/core/ngx_inet.c @ 2200:fd15c193572d
split ngx_parse_url()
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 22 Aug 2008 13:36:56 +0000 |
parents | 5975975eedc0 |
children | 2300ab9b069e |
comparison
equal
deleted
inserted
replaced
2199:ffb512f0eabd | 2200:fd15c193572d |
---|---|
4 */ | 4 */ |
5 | 5 |
6 | 6 |
7 #include <ngx_config.h> | 7 #include <ngx_config.h> |
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 | |
10 | |
11 static ngx_int_t ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u); | |
12 static ngx_int_t ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u); | |
9 | 13 |
10 | 14 |
11 /* AF_INET only */ | 15 /* AF_INET only */ |
12 | 16 |
13 in_addr_t | 17 in_addr_t |
147 | 151 |
148 | 152 |
149 ngx_int_t | 153 ngx_int_t |
150 ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u) | 154 ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u) |
151 { | 155 { |
152 u_char *p, *host, *port_start; | 156 u_char *p; |
153 size_t len, port_len; | 157 |
154 ngx_int_t port; | 158 p = u->url.data; |
159 | |
160 if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) { | |
161 return ngx_parse_unix_domain_url(pool, u); | |
162 } | |
163 | |
164 if ((p[0] == ':' || p[0] == '/') && !u->listen) { | |
165 u->err = "invalid host"; | |
166 return NGX_ERROR; | |
167 } | |
168 | |
169 return ngx_parse_inet_url(pool, u); | |
170 } | |
171 | |
172 | |
173 static ngx_int_t | |
174 ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u) | |
175 { | |
176 #if (NGX_HAVE_UNIX_DOMAIN) | |
177 u_char *p; | |
178 size_t len; | |
155 ngx_uint_t i; | 179 ngx_uint_t i; |
156 struct hostent *h; | |
157 #if (NGX_HAVE_UNIX_DOMAIN) | |
158 struct sockaddr_un *saun; | 180 struct sockaddr_un *saun; |
159 #endif | |
160 | 181 |
161 len = u->url.len; | 182 len = u->url.len; |
162 p = u->url.data; | 183 p = u->url.data; |
163 | 184 |
164 if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) { | 185 p += 5; |
165 | 186 len -= 5; |
166 #if (NGX_HAVE_UNIX_DOMAIN) | 187 |
167 | 188 u->uri.len = len; |
168 p += 5; | 189 u->uri.data = p; |
169 len -= 5; | 190 |
170 | 191 if (u->uri_part) { |
171 u->uri.len = len; | 192 for (i = 0; i < len; i++) { |
172 u->uri.data = p; | 193 |
173 | 194 if (p[i] == ':') { |
174 if (u->uri_part) { | 195 len = i; |
175 for (i = 0; i < len; i++) { | 196 |
176 | 197 u->uri.len -= len + 1; |
177 if (p[i] == ':') { | 198 u->uri.data += len + 1; |
178 len = i; | 199 |
179 | 200 break; |
180 u->uri.len -= len + 1; | 201 } |
181 u->uri.data += len + 1; | 202 } |
182 | 203 } |
183 break; | 204 |
184 } | 205 if (len == 0) { |
185 } | 206 u->err = "no path in the unix domain socket"; |
186 } | 207 return NGX_ERROR; |
187 | 208 } |
188 if (len == 0) { | 209 |
189 u->err = "no path in the unix domain socket"; | 210 if (len + 1 > sizeof(saun->sun_path)) { |
190 return NGX_ERROR; | 211 u->err = "too long path in the unix domain socket"; |
191 } | 212 return NGX_ERROR; |
192 | 213 } |
193 if (len + 1 > sizeof(saun->sun_path)) { | 214 |
194 u->err = "too long path in the unix domain socket"; | 215 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t)); |
195 return NGX_ERROR; | 216 if (u->addrs == NULL) { |
196 } | 217 return NGX_ERROR; |
197 | 218 } |
198 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t)); | 219 |
199 if (u->addrs == NULL) { | 220 saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un)); |
200 return NGX_ERROR; | 221 if (saun == NULL) { |
201 } | 222 return NGX_ERROR; |
202 | 223 } |
203 saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un)); | 224 |
204 if (saun == NULL) { | 225 u->naddrs = 1; |
205 return NGX_ERROR; | 226 |
206 } | 227 saun->sun_family = AF_UNIX; |
207 | 228 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1); |
208 u->naddrs = 1; | 229 |
209 | 230 u->addrs[0].sockaddr = (struct sockaddr *) saun; |
210 saun->sun_family = AF_UNIX; | 231 u->addrs[0].socklen = sizeof(struct sockaddr_un); |
211 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1); | 232 u->addrs[0].name.len = len + 5; |
212 | 233 u->addrs[0].name.data = u->url.data; |
213 u->addrs[0].sockaddr = (struct sockaddr *) saun; | 234 |
214 u->addrs[0].socklen = sizeof(struct sockaddr_un); | 235 u->host.len = len; |
215 u->addrs[0].name.len = len + 5; | 236 u->host.data = p; |
216 u->addrs[0].name.data = u->url.data; | 237 |
217 | 238 u->unix_socket = 1; |
218 u->host.len = len; | 239 |
219 u->host.data = p; | 240 return NGX_OK; |
220 | |
221 u->unix_socket = 1; | |
222 | |
223 return NGX_OK; | |
224 | 241 |
225 #else | 242 #else |
226 u->err = "the unix domain sockets are not supported on this platform"; | 243 |
227 | 244 u->err = "the unix domain sockets are not supported on this platform"; |
228 return NGX_ERROR; | 245 |
246 return NGX_ERROR; | |
229 | 247 |
230 #endif | 248 #endif |
231 } | 249 } |
232 | 250 |
233 if ((p[0] == ':' || p[0] == '/') && !u->listen) { | 251 |
234 u->err = "invalid host"; | 252 static ngx_int_t |
235 return NGX_ERROR; | 253 ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u) |
236 } | 254 { |
255 u_char *p, *host, *port_start; | |
256 size_t len, port_len; | |
257 ngx_int_t port; | |
258 ngx_uint_t i; | |
259 struct hostent *h; | |
260 | |
261 len = u->url.len; | |
262 p = u->url.data; | |
237 | 263 |
238 u->host.data = p; | 264 u->host.data = p; |
239 | 265 |
240 port_start = NULL; | 266 port_start = NULL; |
241 port_len = 0; | 267 port_len = 0; |