2 * uhttpd - Tiny non-forking httpd - Utility functions
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "uhttpd-utils.h"
23 #include "uhttpd-tls.h"
27 static char *uh_index_files[] = {
35 const char * sa_straddr(void *sa)
37 static char str[INET6_ADDRSTRLEN];
38 struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
39 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)sa;
41 if( v4->sin_family == AF_INET )
42 return inet_ntop(AF_INET, &(v4->sin_addr), str, sizeof(str));
44 return inet_ntop(AF_INET6, &(v6->sin6_addr), str, sizeof(str));
47 const char * sa_strport(void *sa)
50 snprintf(str, sizeof(str), "%i", sa_port(sa));
56 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
59 /* Simple strstr() like function that takes len arguments for both haystack and needle. */
60 char *strfind(char *haystack, int hslen, const char *needle, int ndlen)
65 for( i = 0; i < hslen; i++ )
67 if( haystack[i] == needle[0] )
69 match = ((ndlen == 1) || ((i + ndlen) <= hslen));
71 for( j = 1; (j < ndlen) && ((i + j) < hslen); j++ )
73 if( haystack[i+j] != needle[j] )
89 int uh_tcp_send(struct client *cl, const char *buf, int len)
92 struct timeval timeout;
95 FD_SET(cl->socket, &writer);
98 timeout.tv_usec = 500000;
100 if( select(cl->socket + 1, NULL, &writer, NULL, &timeout) > 0 )
104 return SSL_write(cl->tls, buf, len);
107 return send(cl->socket, buf, len, 0);
113 int uh_tcp_peek(struct client *cl, char *buf, int len)
115 int sz = uh_tcp_recv(cl, buf, len);
117 /* store received data in peek buffer */
121 memcpy(cl->peekbuf, buf, sz);
127 int uh_tcp_recv(struct client *cl, char *buf, int len)
132 /* first serve data from peek buffer */
133 if( cl->peeklen > 0 )
135 sz = min(cl->peeklen, len);
136 len -= sz; cl->peeklen -= sz;
138 memcpy(buf, cl->peekbuf, sz);
139 memmove(cl->peekbuf, &cl->peekbuf[sz], cl->peeklen);
142 /* caller wants more */
147 rsz = SSL_read(cl->tls, (void *)&buf[sz], len);
150 rsz = recv(cl->socket, (void *)&buf[sz], len, 0);
152 if( (sz == 0) || (rsz > 0) )
160 do { if( x < 0 ) return -1; } while(0)
162 int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...)
166 char buffer[UH_LIMIT_MSGHEAD];
169 len = snprintf(buffer, sizeof(buffer),
170 "HTTP/1.1 %03i %s\r\n"
171 "Content-Type: text/plain\r\n"
172 "Transfer-Encoding: chunked\r\n\r\n",
176 ensure(uh_tcp_send(cl, buffer, len));
179 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
182 ensure(uh_http_sendc(cl, buffer, len));
183 ensure(uh_http_sendc(cl, NULL, 0));
189 int uh_http_sendc(struct client *cl, const char *data, int len)
199 clen = snprintf(chunk, sizeof(chunk), "%X\r\n", len);
200 ensure(uh_tcp_send(cl, chunk, clen));
201 ensure(uh_tcp_send(cl, data, len));
202 ensure(uh_tcp_send(cl, "\r\n", 2));
206 ensure(uh_tcp_send(cl, "0\r\n\r\n", 5));
213 struct client *cl, struct http_request *req, const char *fmt, ...
216 char buffer[UH_LIMIT_MSGHEAD];
220 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
223 if( (req != NULL) && (req->version > 1.0) )
224 ensure(uh_http_sendc(cl, buffer, len));
226 ensure(uh_tcp_send(cl, buffer, len));
232 struct client *cl, struct http_request *req, const char *buf, int len
237 if( (req != NULL) && (req->version > 1.0) )
238 ensure(uh_http_sendc(cl, buf, len));
240 ensure(uh_tcp_send(cl, buf, len));
246 int uh_urldecode(char *buf, int blen, const char *src, int slen)
252 (((x) <= '9') ? ((x) - '0') : \
253 (((x) <= 'F') ? ((x) - 'A' + 10) : \
256 for( i = 0; (i <= slen) && (i <= blen); i++ )
260 if( ((i+2) <= slen) && isxdigit(src[i+1]) && isxdigit(src[i+2]) )
262 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
279 int uh_urlencode(char *buf, int blen, const char *src, int slen)
283 const char hex[] = "0123456789abcdef";
285 for( i = 0; (i <= slen) && (i <= blen); i++ )
287 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
288 (src[i] == '.') || (src[i] == '~') )
292 else if( (len+3) <= blen )
295 buf[len++] = hex[(src[i] >> 4) & 15];
296 buf[len++] = hex[(src[i] & 15) & 15];
307 int uh_path_normalize(char *buf, int blen, const char *src, int slen)
312 for( i = 0, skip = 1; (i <= slen) && (src[i] != 0); i++ )
314 /* collapse multiple "/" into one */
317 /* collapse "/../" to "/" */
318 if( ((i+2) <= slen) && (src[i+1] == '.') && (src[i+2] == '.') &&
319 (((i+3) > slen) || (src[i+3] == '/'))
325 /* collapse "/./" to "/" */
326 else if( ((i+1) <= slen) && (src[i+1] == '.') &&
327 (((i+2) > slen) || (src[i+2] == '/'))
333 /* skip repeating "/" */
342 /* finally a harmless char */
355 struct uh_path_info * uh_path_lookup(struct client *cl, const char *url)
357 static char path_phys[PATH_MAX];
358 static char path_info[PATH_MAX];
359 static struct uh_path_info p;
361 char buffer[UH_LIMIT_MSGHEAD];
362 char *docroot = cl->server->conf->docroot;
363 char *pathptr = NULL;
369 memset(path_phys, 0, sizeof(path_phys));
370 memset(path_info, 0, sizeof(path_info));
371 memset(buffer, 0, sizeof(buffer));
372 memset(&p, 0, sizeof(p));
375 memcpy(buffer, docroot, sizeof(buffer));
377 /* separate query string from url */
378 if( (pathptr = strchr(url, '?')) != NULL )
380 p.query = pathptr[1] ? pathptr + 1 : NULL;
382 /* urldecode component w/o query */
385 &buffer[strlen(docroot)],
386 sizeof(buffer) - strlen(docroot) - 1,
387 url, (int)(pathptr - url) - 1
391 /* no query string, decode all of url */
395 &buffer[strlen(docroot)],
396 sizeof(buffer) - strlen(docroot) - 1,
401 /* create canon path */
402 for( i = strlen(buffer); i >= 0; i-- )
404 if( (buffer[i] == 0) || (buffer[i] == '/') )
406 memset(path_info, 0, sizeof(path_info));
407 memcpy(path_info, buffer, min(i + 1, sizeof(path_info) - 1));
409 if( realpath(path_info, path_phys) )
411 memset(path_info, 0, sizeof(path_info));
412 memcpy(path_info, &buffer[i],
413 min(strlen(buffer) - i, sizeof(path_info) - 1));
420 /* check whether found path is within docroot */
421 if( strncmp(path_phys, docroot, strlen(docroot)) ||
422 ((path_phys[strlen(docroot)] != 0) &&
423 (path_phys[strlen(docroot)] != '/'))
428 /* test current path */
429 if( ! stat(path_phys, &p.stat) )
431 /* is a regular file */
432 if( p.stat.st_mode & S_IFREG )
436 p.name = &path_phys[strlen(docroot)];
437 p.info = path_info[0] ? path_info : NULL;
441 else if( (p.stat.st_mode & S_IFDIR) && !strlen(path_info) )
443 /* ensure trailing slash */
444 if( path_phys[strlen(path_phys)-1] != '/' )
445 path_phys[strlen(path_phys)] = '/';
447 /* try to locate index file */
448 memset(buffer, 0, sizeof(buffer));
449 memcpy(buffer, path_phys, sizeof(buffer));
450 pathptr = &buffer[strlen(buffer)];
452 for( i = 0; i < array_size(uh_index_files); i++ )
454 strncat(buffer, uh_index_files[i], sizeof(buffer));
456 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
458 memcpy(path_phys, buffer, sizeof(path_phys));
459 memcpy(&p.stat, &s, sizeof(p.stat));
468 p.name = &path_phys[strlen(docroot)];
472 return p.phys ? &p : NULL;
476 static char uh_listeners[UH_LIMIT_LISTENERS * sizeof(struct listener)] = { 0 };
477 static char uh_clients[UH_LIMIT_CLIENTS * sizeof(struct client)] = { 0 };
479 static int uh_listener_count = 0;
480 static int uh_client_count = 0;
483 struct listener * uh_listener_add(int sock, struct config *conf)
485 struct listener *new = NULL;
488 if( uh_listener_count < UH_LIMIT_LISTENERS )
490 new = (struct listener *)
491 &uh_listeners[uh_listener_count * sizeof(struct listener)];
496 /* get local endpoint addr */
497 sl = sizeof(struct sockaddr_in6);
498 memset(&(new->addr), 0, sl);
499 getsockname(sock, (struct sockaddr *) &(new->addr), &sl);
507 struct listener * uh_listener_lookup(int sock)
509 struct listener *cur = NULL;
512 for( i = 0; i < uh_listener_count; i++ )
514 cur = (struct listener *) &uh_listeners[i * sizeof(struct listener)];
516 if( cur->socket == sock )
524 struct client * uh_client_add(int sock, struct listener *serv)
526 struct client *new = NULL;
529 if( uh_client_count < UH_LIMIT_CLIENTS )
531 new = (struct client *)
532 &uh_clients[uh_client_count * sizeof(struct client)];
537 /* get remote endpoint addr */
538 sl = sizeof(struct sockaddr_in6);
539 memset(&(new->peeraddr), 0, sl);
540 getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);
542 /* get local endpoint addr */
543 sl = sizeof(struct sockaddr_in6);
544 memset(&(new->servaddr), 0, sl);
545 getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl);
553 struct client * uh_client_lookup(int sock)
555 struct client *cur = NULL;
558 for( i = 0; i < uh_client_count; i++ )
560 cur = (struct client *) &uh_clients[i * sizeof(struct client)];
562 if( cur->socket == sock )
569 void uh_client_remove(int sock)
571 struct client *del = uh_client_lookup(sock);
575 memmove(del, del + 1,
576 sizeof(uh_clients) - (int)((char *)del - uh_clients) - sizeof(struct client));