Changeset 32704
- Timestamp:
- 2012-07-13T19:10:56+02:00 (6 years ago)
- Location:
- trunk/package/uhttpd
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/package/uhttpd/Makefile
r32662 r32704 9 9 10 10 PKG_NAME:=uhttpd 11 PKG_RELEASE:= 3911 PKG_RELEASE:=40 12 12 13 13 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) -
trunk/package/uhttpd/src/uhttpd-cgi.c
r32662 r32704 202 202 /* write status */ 203 203 ensure_out(uh_http_sendf(cl, NULL, 204 " HTTP/%.1f%03d %s\r\n"204 "%s %03d %s\r\n" 205 205 "Connection: close\r\n", 206 req->version, res->statuscode, res->statusmsg)); 206 http_versions[req->version], 207 res->statuscode, res->statusmsg)); 207 208 208 209 /* add Content-Type if no Location or Content-Type */ … … 215 216 216 217 /* if request was HTTP 1.1 we'll respond chunked */ 217 if ((req->version > 1.0) &&218 if ((req->version > UH_HTTP_VER_1_0) && 218 219 !uh_cgi_header_lookup(res, "Transfer-Encoding")) 219 220 { … … 261 262 262 263 ensure_out(uh_http_sendf(cl, NULL, 263 " HTTP/%.1f200 OK\r\n"264 "%s 200 OK\r\n" 264 265 "Content-Type: text/plain\r\n" 265 266 "%s\r\n", 266 req->version, (req->version > 1.0) 267 http_versions[req->version], 268 (req->version > UH_HTTP_VER_1_0) 267 269 ? "Transfer-Encoding: chunked\r\n" : "" 268 270 )); … … 428 430 429 431 /* http version */ 430 if (req->version > 1.0) 431 setenv("SERVER_PROTOCOL", "HTTP/1.1", 1); 432 else 433 setenv("SERVER_PROTOCOL", "HTTP/1.0", 1); 432 setenv("SERVER_PROTOCOL", http_versions[req->version], 1); 434 433 435 434 /* request method */ 436 switch (req->method) 437 { 438 case UH_HTTP_MSG_GET: 439 setenv("REQUEST_METHOD", "GET", 1); 440 break; 441 442 case UH_HTTP_MSG_HEAD: 443 setenv("REQUEST_METHOD", "HEAD", 1); 444 break; 445 446 case UH_HTTP_MSG_POST: 447 setenv("REQUEST_METHOD", "POST", 1); 448 break; 449 } 435 setenv("REQUEST_METHOD", http_methods[req->method], 1); 450 436 451 437 /* request url */ -
trunk/package/uhttpd/src/uhttpd-file.c
r31931 r32704 114 114 static int uh_file_response_200(struct client *cl, struct stat *s) 115 115 { 116 ensure_ret(uh_http_sendf(cl, NULL, " HTTP/%.1f200 OK\r\n",117 cl->request.version));116 ensure_ret(uh_http_sendf(cl, NULL, "%s 200 OK\r\n", 117 http_versions[cl->request.version])); 118 118 119 119 return uh_file_response_ok_hdrs(cl, s); … … 122 122 static int uh_file_response_304(struct client *cl, struct stat *s) 123 123 { 124 ensure_ret(uh_http_sendf(cl, NULL, " HTTP/%.1f304 Not Modified\r\n",125 cl->request.version));124 ensure_ret(uh_http_sendf(cl, NULL, "%s 304 Not Modified\r\n", 125 http_versions[cl->request.version])); 126 126 127 127 return uh_file_response_ok_hdrs(cl, s); … … 131 131 { 132 132 return uh_http_sendf(cl, NULL, 133 "HTTP/%.1f 412 Precondition Failed\r\n" 134 "Connection: close\r\n", cl->request.version); 133 "%s 412 Precondition Failed\r\n" 134 "Connection: close\r\n", 135 http_versions[cl->request.version]); 135 136 } 136 137 -
trunk/package/uhttpd/src/uhttpd-lua.c
r32651 r32704 67 67 } 68 68 69 static int uh_lua_send_common(lua_State *L, intchunked)69 static int uh_lua_send_common(lua_State *L, bool chunked) 70 70 { 71 71 size_t length; … … 113 113 static int uh_lua_send(lua_State *L) 114 114 { 115 return uh_lua_send_common(L, 0);115 return uh_lua_send_common(L, false); 116 116 } 117 117 118 118 static int uh_lua_sendc(lua_State *L) 119 119 { 120 return uh_lua_send_common(L, 1);120 return uh_lua_send_common(L, true); 121 121 } 122 122 … … 415 415 416 416 /* request method */ 417 switch(req->method) 418 { 419 case UH_HTTP_MSG_GET: 420 lua_pushstring(L, "GET"); 421 break; 422 423 case UH_HTTP_MSG_HEAD: 424 lua_pushstring(L, "HEAD"); 425 break; 426 427 case UH_HTTP_MSG_POST: 428 lua_pushstring(L, "POST"); 429 break; 430 } 431 417 lua_pushstring(L, http_methods[req->method]); 432 418 lua_setfield(L, -2, "REQUEST_METHOD"); 433 419 … … 463 449 464 450 /* http protcol version */ 465 lua_pushnumber(L, floor(req->version * 10) / 10);451 lua_pushnumber(L, 0.9 + (req->version / 10.0)); 466 452 lua_setfield(L, -2, "HTTP_VERSION"); 467 453 468 if (req->version > 1.0) 469 lua_pushstring(L, "HTTP/1.1"); 470 else 471 lua_pushstring(L, "HTTP/1.0"); 472 454 lua_pushstring(L, http_versions[req->version]); 473 455 lua_setfield(L, -2, "SERVER_PROTOCOL"); 474 456 … … 530 512 err_str = "Unknown error"; 531 513 532 printf(" HTTP/%.1f500 Internal Server Error\r\n"514 printf("%s 500 Internal Server Error\r\n" 533 515 "Connection: close\r\n" 534 516 "Content-Type: text/plain\r\n" 535 517 "Content-Length: %i\r\n\r\n" 536 518 "Lua raised a runtime error:\n %s\n", 537 req->version, 31 + strlen(err_str), err_str); 519 http_versions[req->version], 520 31 + strlen(err_str), err_str); 538 521 539 522 break; -
trunk/package/uhttpd/src/uhttpd-utils.c
r32651 r32704 335 335 va_end(ap); 336 336 337 if ((req != NULL) && (req->version > 1.0))337 if ((req != NULL) && (req->version > UH_HTTP_VER_1_0)) 338 338 ensure_ret(uh_http_sendc(cl, buffer, len)); 339 339 else if (len > 0) … … 349 349 len = strlen(buf); 350 350 351 if ((req != NULL) && (req->version > 1.0))351 if ((req != NULL) && (req->version > UH_HTTP_VER_1_0)) 352 352 ensure_ret(uh_http_sendc(cl, buf, len)); 353 353 else if (len > 0) … … 866 866 /* 401 */ 867 867 uh_http_sendf(cl, NULL, 868 "HTTP/%.1f401 Authorization Required\r\n"869 870 871 872 873 req->version, cl->server->conf->realm874 );868 "%s 401 Authorization Required\r\n" 869 "WWW-Authenticate: Basic realm=\"%s\"\r\n" 870 "Content-Type: text/plain\r\n" 871 "Content-Length: 23\r\n\r\n" 872 "Authorization Required\n", 873 http_versions[req->version], 874 cl->server->conf->realm); 875 875 876 876 return 0; … … 923 923 924 924 925 struct client * uh_client_add(int sock, struct listener *serv) 925 struct client * uh_client_add(int sock, struct listener *serv, 926 struct sockaddr_in6 *peer) 926 927 { 927 928 struct client *new = NULL; … … 931 932 { 932 933 memset(new, 0, sizeof(struct client)); 934 memcpy(&new->peeraddr, peer, sizeof(new->peeraddr)); 933 935 934 936 new->fd.fd = sock; … … 938 940 new->wpipe.fd = -1; 939 941 940 /* get remote endpoint addr */941 sl = sizeof(struct sockaddr_in6);942 memset(&(new->peeraddr), 0, sl);943 getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);944 945 942 /* get local endpoint addr */ 946 943 sl = sizeof(struct sockaddr_in6); 947 memset(&(new->servaddr), 0, sl);948 944 getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl); 949 945 … … 989 985 for (cur = uh_clients; cur; prv = cur, cur = cur->next) 990 986 { 991 if ( (cur == cl) || (!cl && cur->dead))987 if (cur == cl) 992 988 { 993 989 if (prv) -
trunk/package/uhttpd/src/uhttpd-utils.h
r32651 r32704 116 116 struct listener * uh_listener_lookup(int sock); 117 117 118 struct client * uh_client_add(int sock, struct listener *serv); 118 struct client * uh_client_add(int sock, struct listener *serv, 119 struct sockaddr_in6 *peer); 120 119 121 struct client * uh_client_lookup(int sock); 120 122 -
trunk/package/uhttpd/src/uhttpd.c
r32651 r32704 36 36 37 37 38 const char * http_methods[] = { "GET", "POST", "HEAD", }; 39 const char * http_versions[] = { "HTTP/0.9", "HTTP/1.0", "HTTP/1.1", }; 40 38 41 static int run = 1; 39 42 … … 124 127 static void uh_listener_cb(struct uloop_fd *u, unsigned int events); 125 128 126 static int uh_socket_bind(fd_set *serv_fds, int *max_fd, 127 const char *host, const char *port, 128 struct addrinfo *hints, int do_tls, 129 struct config *conf) 129 static int uh_socket_bind(const char *host, const char *port, 130 struct addrinfo *hints, int do_tls, 131 struct config *conf) 130 132 { 131 133 int sock = -1; … … 214 216 #endif 215 217 216 /* add socket to server fd set */ 217 FD_SET(sock, serv_fds); 218 /* add socket to uloop */ 218 219 fd_cloexec(sock); 219 *max_fd = max(*max_fd, sock);220 221 220 uh_ufd_add(&l->fd, uh_listener_cb, ULOOP_READ); 222 221 … … 269 268 270 269 /* check method */ 271 if (strcmp(method, "GET") && strcmp(method, "HEAD") && strcmp(method, "POST")) 270 if (method && !strcmp(method, "GET")) 271 req->method = UH_HTTP_MSG_GET; 272 else if (method && !strcmp(method, "POST")) 273 req->method = UH_HTTP_MSG_POST; 274 else if (method && !strcmp(method, "HEAD")) 275 req->method = UH_HTTP_MSG_HEAD; 276 else 272 277 { 273 278 /* invalid method */ … … 275 280 return NULL; 276 281 } 277 else278 {279 switch(method[0])280 {281 case 'G':282 req->method = UH_HTTP_MSG_GET;283 break;284 285 case 'H':286 req->method = UH_HTTP_MSG_HEAD;287 break;288 289 case 'P':290 req->method = UH_HTTP_MSG_POST;291 break;292 }293 }294 282 295 283 /* check path */ … … 306 294 307 295 /* check version */ 308 if ((version == NULL) || (strcmp(version, "HTTP/0.9") && 309 strcmp(version, "HTTP/1.0") && strcmp(version, "HTTP/1.1"))) 296 if (version && !strcmp(version, "HTTP/0.9")) 297 req->version = UH_HTTP_VER_0_9; 298 else if (version && !strcmp(version, "HTTP/1.0")) 299 req->version = UH_HTTP_VER_1_0; 300 else if (version && !strcmp(version, "HTTP/1.1")) 301 req->version = UH_HTTP_VER_1_1; 302 else 310 303 { 311 304 /* unsupported version */ … … 313 306 return NULL; 314 307 } 315 else 316 { 317 req->version = strtof(&version[5], NULL); 318 } 319 320 D("SRV: %s %s HTTP/%.1f\n", 321 (req->method == UH_HTTP_MSG_POST) ? "POST" : 322 (req->method == UH_HTTP_MSG_GET) ? "GET" : "HEAD", 323 req->url, req->version); 308 309 D("SRV: %s %s %s\n", 310 http_methods[req->method], req->url, http_versions[req->version]); 324 311 325 312 /* process header fields */ … … 523 510 struct config *conf; 524 511 512 struct sockaddr_in6 sa; 513 socklen_t sl = sizeof(sa); 514 525 515 serv = container_of(u, struct listener, fd); 526 516 conf = serv->conf; … … 531 521 532 522 /* handle new connections */ 533 if ((new_fd = accept(u->fd, NULL, 0)) != -1)523 if ((new_fd = accept(u->fd, (struct sockaddr *)&sa, &sl)) != -1) 534 524 { 535 525 D("SRV: Server(%d) accept => Client(%d)\n", u->fd, new_fd); 536 526 537 527 /* add to global client list */ 538 if ((cl = uh_client_add(new_fd, serv )) != NULL)528 if ((cl = uh_client_add(new_fd, serv, &sa)) != NULL) 539 529 { 540 530 /* add client socket to global fdset */ … … 786 776 int main (int argc, char **argv) 787 777 { 788 /* master file descriptor list */789 fd_set serv_fds;790 791 778 /* working structs */ 792 779 struct addrinfo hints; … … 795 782 796 783 /* maximum file descriptor number */ 797 int cur_fd , max_fd= 0;784 int cur_fd = 0; 798 785 799 786 #ifdef HAVE_TLS … … 807 794 /* args */ 808 795 int opt; 809 char bind[128];796 char addr[128]; 810 797 char *port = NULL; 811 798 812 #if def HAVE_LUA799 #if defined(HAVE_LUA) || defined(HAVE_TLS) || defined(HAVE_UBUS) 813 800 /* library handle */ 814 801 void *lib; 815 802 #endif 816 817 FD_ZERO(&serv_fds);818 803 819 804 /* handle SIGPIPE, SIGINT, SIGTERM */ … … 836 821 /* parse args */ 837 822 memset(&conf, 0, sizeof(conf)); 838 memset(bind, 0, sizeof(bind));839 823 840 824 uloop_init(); … … 848 832 case 'p': 849 833 case 's': 834 memset(addr, 0, sizeof(addr)); 835 850 836 if ((port = strrchr(optarg, ':')) != NULL) 851 837 { 852 838 if ((optarg[0] == '[') && (port > optarg) && (port[-1] == ']')) 853 memcpy( bind, optarg + 1,854 min(sizeof( bind), (int)(port - optarg) - 2));839 memcpy(addr, optarg + 1, 840 min(sizeof(addr), (int)(port - optarg) - 2)); 855 841 else 856 memcpy( bind, optarg,857 min(sizeof( bind), (int)(port - optarg)));842 memcpy(addr, optarg, 843 min(sizeof(addr), (int)(port - optarg))); 858 844 859 845 port++; … … 881 867 882 868 /* bind sockets */ 883 bound += uh_socket_bind(&serv_fds, &max_fd, 884 bind[0] ? bind : NULL, 885 port, &hints, (opt == 's'), &conf); 886 887 memset(bind, 0, sizeof(bind)); 869 bound += uh_socket_bind(addr[0] ? addr : NULL, port, &hints, 870 (opt == 's'), &conf); 888 871 break; 889 872 … … 920 903 921 904 break; 905 #else 906 case 'C': 907 case 'K': 908 fprintf(stderr, 909 "Notice: TLS support not compiled, ignoring -%c\n", 910 opt); 911 break; 922 912 #endif 923 913 … … 992 982 } 993 983 break; 984 #else 985 case 'x': 986 case 'i': 987 fprintf(stderr, 988 "Notice: CGI support not compiled, ignoring -%c\n", 989 opt); 990 break; 994 991 #endif 995 992 … … 1004 1001 conf.lua_handler = optarg; 1005 1002 break; 1003 #else 1004 case 'l': 1005 case 'L': 1006 fprintf(stderr, 1007 "Notice: Lua support not compiled, ignoring -%c\n", 1008 opt); 1009 break; 1006 1010 #endif 1007 1011 … … 1015 1019 case 'U': 1016 1020 conf.ubus_socket = optarg; 1021 break; 1022 #else 1023 case 'u': 1024 case 'U': 1025 fprintf(stderr, 1026 "Notice: UBUS support not compiled, ignoring -%c\n", 1027 opt); 1017 1028 break; 1018 1029 #endif -
trunk/package/uhttpd/src/uhttpd.h
r32651 r32704 64 64 #define UH_LIMIT_MSGHEAD 4096 65 65 #define UH_LIMIT_HEADERS 64 66 67 66 #define UH_LIMIT_CLIENTS 64 68 67 69 #define UH_HTTP_MSG_GET 070 #define UH_HTTP_MSG_HEAD 171 #define UH_HTTP_MSG_POST 272 73 #define UH_SOCK_CLIENT 074 #define UH_SOCK_SERVER 175 68 76 69 struct listener; … … 129 122 }; 130 123 124 enum http_method { 125 UH_HTTP_MSG_GET, 126 UH_HTTP_MSG_POST, 127 UH_HTTP_MSG_HEAD, 128 }; 129 130 extern const char *http_methods[]; 131 132 enum http_version { 133 UH_HTTP_VER_0_9, 134 UH_HTTP_VER_1_0, 135 UH_HTTP_VER_1_1, 136 }; 137 138 extern const char *http_versions[]; 139 131 140 struct http_request { 132 intmethod;133 floatversion;141 enum http_method method; 142 enum http_version version; 134 143 int redirect_status; 135 144 char *url; … … 168 177 void *priv; 169 178 bool dispatched; 170 bool dead;171 179 struct { 172 180 char buf[UH_LIMIT_MSGHEAD];
Note: See TracChangeset
for help on using the changeset viewer.