| 1 | /* |
|---|
| 2 | ** Copyright (c) 2002 Hughes Technologies Pty Ltd. All rights |
|---|
| 3 | ** reserved. |
|---|
| 4 | ** |
|---|
| 5 | ** Terms under which this software may be used or copied are |
|---|
| 6 | ** provided in the specific license associated with this product. |
|---|
| 7 | ** |
|---|
| 8 | ** Hughes Technologies disclaims all warranties with regard to this |
|---|
| 9 | ** software, including all implied warranties of merchantability and |
|---|
| 10 | ** fitness, in no event shall Hughes Technologies be liable for any |
|---|
| 11 | ** special, indirect or consequential damages or any damages whatsoever |
|---|
| 12 | ** resulting from loss of use, data or profits, whether in an action of |
|---|
| 13 | ** contract, negligence or other tortious action, arising out of or in |
|---|
| 14 | ** connection with the use or performance of this software. |
|---|
| 15 | ** |
|---|
| 16 | ** |
|---|
| 17 | ** $Id$ |
|---|
| 18 | ** |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include <stdlib.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <ctype.h> |
|---|
| 25 | #include <sys/types.h> |
|---|
| 26 | #include <sys/stat.h> |
|---|
| 27 | #include <time.h> |
|---|
| 28 | |
|---|
| 29 | #if defined(_WIN32) |
|---|
| 30 | #include <winsock2.h> |
|---|
| 31 | #else |
|---|
| 32 | #include <unistd.h> |
|---|
| 33 | #include <sys/file.h> |
|---|
| 34 | #include <netinet/in.h> |
|---|
| 35 | #include <arpa/inet.h> |
|---|
| 36 | #include <netdb.h> |
|---|
| 37 | #include <sys/socket.h> |
|---|
| 38 | #include <netdb.h> |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #include "config.h" |
|---|
| 42 | #include "httpd.h" |
|---|
| 43 | #include "httpd_priv.h" |
|---|
| 44 | |
|---|
| 45 | #ifdef HAVE_STDARG_H |
|---|
| 46 | # include <stdarg.h> |
|---|
| 47 | #else |
|---|
| 48 | # include <varargs.h> |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | char *httpdUrlEncode(str) |
|---|
| 53 | char *str; |
|---|
| 54 | { |
|---|
| 55 | char *new, |
|---|
| 56 | *cp; |
|---|
| 57 | |
|---|
| 58 | new = (char *)_httpd_escape(str); |
|---|
| 59 | if (new == NULL) |
|---|
| 60 | { |
|---|
| 61 | return(NULL); |
|---|
| 62 | } |
|---|
| 63 | cp = new; |
|---|
| 64 | while(*cp) |
|---|
| 65 | { |
|---|
| 66 | if (*cp == ' ') |
|---|
| 67 | *cp = '+'; |
|---|
| 68 | cp++; |
|---|
| 69 | } |
|---|
| 70 | return(new); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | char *httpdRequestMethodName(server) |
|---|
| 76 | httpd *server; |
|---|
| 77 | { |
|---|
| 78 | static char tmpBuf[255]; |
|---|
| 79 | |
|---|
| 80 | switch(server->request.method) |
|---|
| 81 | { |
|---|
| 82 | case HTTP_GET: return("GET"); |
|---|
| 83 | case HTTP_POST: return("POST"); |
|---|
| 84 | default: |
|---|
| 85 | snprintf(tmpBuf,255,"Invalid method '%d'", |
|---|
| 86 | server->request.method); |
|---|
| 87 | return(tmpBuf); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | httpVar *httpdGetVariableByName(server, name) |
|---|
| 93 | httpd *server; |
|---|
| 94 | char *name; |
|---|
| 95 | { |
|---|
| 96 | httpVar *curVar; |
|---|
| 97 | |
|---|
| 98 | curVar = server->variables; |
|---|
| 99 | while(curVar) |
|---|
| 100 | { |
|---|
| 101 | if (strcmp(curVar->name, name) == 0) |
|---|
| 102 | return(curVar); |
|---|
| 103 | curVar = curVar->nextVariable; |
|---|
| 104 | } |
|---|
| 105 | return(NULL); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | httpVar *httpdGetVariableByPrefix(server, prefix) |
|---|
| 111 | httpd *server; |
|---|
| 112 | char *prefix; |
|---|
| 113 | { |
|---|
| 114 | httpVar *curVar; |
|---|
| 115 | |
|---|
| 116 | if (prefix == NULL) |
|---|
| 117 | return(server->variables); |
|---|
| 118 | curVar = server->variables; |
|---|
| 119 | while(curVar) |
|---|
| 120 | { |
|---|
| 121 | if (strncmp(curVar->name, prefix, strlen(prefix)) == 0) |
|---|
| 122 | return(curVar); |
|---|
| 123 | curVar = curVar->nextVariable; |
|---|
| 124 | } |
|---|
| 125 | return(NULL); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | httpVar *httpdGetVariableByPrefixedName(server, prefix, name) |
|---|
| 130 | httpd *server; |
|---|
| 131 | char *prefix, |
|---|
| 132 | *name; |
|---|
| 133 | { |
|---|
| 134 | httpVar *curVar; |
|---|
| 135 | int prefixLen; |
|---|
| 136 | |
|---|
| 137 | if (prefix == NULL) |
|---|
| 138 | return(server->variables); |
|---|
| 139 | curVar = server->variables; |
|---|
| 140 | prefixLen = strlen(prefix); |
|---|
| 141 | while(curVar) |
|---|
| 142 | { |
|---|
| 143 | if (strncmp(curVar->name, prefix, prefixLen) == 0 && |
|---|
| 144 | strcmp(curVar->name + prefixLen, name) == 0) |
|---|
| 145 | { |
|---|
| 146 | return(curVar); |
|---|
| 147 | } |
|---|
| 148 | curVar = curVar->nextVariable; |
|---|
| 149 | } |
|---|
| 150 | return(NULL); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | httpVar *httpdGetNextVariableByPrefix(curVar, prefix) |
|---|
| 155 | httpVar *curVar; |
|---|
| 156 | char *prefix; |
|---|
| 157 | { |
|---|
| 158 | if(curVar) |
|---|
| 159 | curVar = curVar->nextVariable; |
|---|
| 160 | while(curVar) |
|---|
| 161 | { |
|---|
| 162 | if (strncmp(curVar->name, prefix, strlen(prefix)) == 0) |
|---|
| 163 | return(curVar); |
|---|
| 164 | curVar = curVar->nextVariable; |
|---|
| 165 | } |
|---|
| 166 | return(NULL); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | int httpdAddVariable(server, name, value) |
|---|
| 171 | httpd *server; |
|---|
| 172 | char *name, |
|---|
| 173 | *value; |
|---|
| 174 | { |
|---|
| 175 | httpVar *curVar, *lastVar, *newVar; |
|---|
| 176 | |
|---|
| 177 | while(*name == ' ' || *name == '\t') |
|---|
| 178 | name++; |
|---|
| 179 | newVar = malloc(sizeof(httpVar)); |
|---|
| 180 | bzero(newVar, sizeof(httpVar)); |
|---|
| 181 | newVar->name = strdup(name); |
|---|
| 182 | newVar->value = strdup(value); |
|---|
| 183 | lastVar = NULL; |
|---|
| 184 | curVar = server->variables; |
|---|
| 185 | while(curVar) |
|---|
| 186 | { |
|---|
| 187 | if (strcmp(curVar->name, name) != 0) |
|---|
| 188 | { |
|---|
| 189 | lastVar = curVar; |
|---|
| 190 | curVar = curVar->nextVariable; |
|---|
| 191 | continue; |
|---|
| 192 | } |
|---|
| 193 | while(curVar) |
|---|
| 194 | { |
|---|
| 195 | lastVar = curVar; |
|---|
| 196 | curVar = curVar->nextValue; |
|---|
| 197 | } |
|---|
| 198 | lastVar->nextValue = newVar; |
|---|
| 199 | return(0); |
|---|
| 200 | } |
|---|
| 201 | if (lastVar) |
|---|
| 202 | lastVar->nextVariable = newVar; |
|---|
| 203 | else |
|---|
| 204 | server->variables = newVar; |
|---|
| 205 | return(0); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | httpd *httpdCreate(host, port) |
|---|
| 209 | char *host; |
|---|
| 210 | int port; |
|---|
| 211 | { |
|---|
| 212 | httpd *new; |
|---|
| 213 | int sock, |
|---|
| 214 | opt; |
|---|
| 215 | struct sockaddr_in addr; |
|---|
| 216 | |
|---|
| 217 | /* |
|---|
| 218 | ** Create the handle and setup it's basic config |
|---|
| 219 | */ |
|---|
| 220 | new = malloc(sizeof(httpd)); |
|---|
| 221 | if (new == NULL) |
|---|
| 222 | return(NULL); |
|---|
| 223 | bzero(new, sizeof(httpd)); |
|---|
| 224 | new->port = port; |
|---|
| 225 | if (host == HTTP_ANY_ADDR) |
|---|
| 226 | new->host = HTTP_ANY_ADDR; |
|---|
| 227 | else |
|---|
| 228 | new->host = strdup(host); |
|---|
| 229 | new->content = (httpDir*)malloc(sizeof(httpDir)); |
|---|
| 230 | bzero(new->content,sizeof(httpDir)); |
|---|
| 231 | new->content->name = strdup(""); |
|---|
| 232 | |
|---|
| 233 | /* |
|---|
| 234 | ** Setup the socket |
|---|
| 235 | */ |
|---|
| 236 | #ifdef _WIN32 |
|---|
| 237 | { |
|---|
| 238 | WORD wVersionRequested; |
|---|
| 239 | WSADATA wsaData; |
|---|
| 240 | int err; |
|---|
| 241 | |
|---|
| 242 | wVersionRequested = MAKEWORD( 2, 2 ); |
|---|
| 243 | |
|---|
| 244 | err = WSAStartup( wVersionRequested, &wsaData ); |
|---|
| 245 | |
|---|
| 246 | /* Found a usable winsock dll? */ |
|---|
| 247 | if( err != 0 ) |
|---|
| 248 | return NULL; |
|---|
| 249 | |
|---|
| 250 | /* |
|---|
| 251 | ** Confirm that the WinSock DLL supports 2.2. |
|---|
| 252 | ** Note that if the DLL supports versions greater |
|---|
| 253 | ** than 2.2 in addition to 2.2, it will still return |
|---|
| 254 | ** 2.2 in wVersion since that is the version we |
|---|
| 255 | ** requested. |
|---|
| 256 | */ |
|---|
| 257 | |
|---|
| 258 | if( LOBYTE( wsaData.wVersion ) != 2 || |
|---|
| 259 | HIBYTE( wsaData.wVersion ) != 2 ) { |
|---|
| 260 | |
|---|
| 261 | /* |
|---|
| 262 | ** Tell the user that we could not find a usable |
|---|
| 263 | ** WinSock DLL. |
|---|
| 264 | */ |
|---|
| 265 | WSACleanup( ); |
|---|
| 266 | return NULL; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | /* The WinSock DLL is acceptable. Proceed. */ |
|---|
| 270 | } |
|---|
| 271 | #endif |
|---|
| 272 | |
|---|
| 273 | sock = socket(AF_INET, SOCK_STREAM, 0); |
|---|
| 274 | if (sock < 0) |
|---|
| 275 | { |
|---|
| 276 | free(new); |
|---|
| 277 | return(NULL); |
|---|
| 278 | } |
|---|
| 279 | # ifdef SO_REUSEADDR |
|---|
| 280 | opt = 1; |
|---|
| 281 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt,sizeof(int)); |
|---|
| 282 | # endif |
|---|
| 283 | new->serverSock = sock; |
|---|
| 284 | bzero(&addr, sizeof(addr)); |
|---|
| 285 | addr.sin_family = AF_INET; |
|---|
| 286 | if (new->host == HTTP_ANY_ADDR) |
|---|
| 287 | { |
|---|
| 288 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
|---|
| 289 | } |
|---|
| 290 | else |
|---|
| 291 | { |
|---|
| 292 | addr.sin_addr.s_addr = inet_addr(new->host); |
|---|
| 293 | } |
|---|
| 294 | addr.sin_port = htons((u_short)new->port); |
|---|
| 295 | if (bind(sock,(struct sockaddr *)&addr,sizeof(addr)) <0) |
|---|
| 296 | { |
|---|
| 297 | close(sock); |
|---|
| 298 | free(new); |
|---|
| 299 | return(NULL); |
|---|
| 300 | } |
|---|
| 301 | listen(sock, 128); |
|---|
| 302 | new->startTime = time(NULL); |
|---|
| 303 | return(new); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | void httpdDestroy(server) |
|---|
| 307 | httpd *server; |
|---|
| 308 | { |
|---|
| 309 | if (server == NULL) |
|---|
| 310 | return; |
|---|
| 311 | if (server->host) |
|---|
| 312 | free(server->host); |
|---|
| 313 | free(server); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | int httpdGetConnection(server, timeout) |
|---|
| 319 | httpd *server; |
|---|
| 320 | struct timeval *timeout; |
|---|
| 321 | { |
|---|
| 322 | int result; |
|---|
| 323 | fd_set fds; |
|---|
| 324 | struct sockaddr_in addr; |
|---|
| 325 | size_t addrLen; |
|---|
| 326 | char *ipaddr; |
|---|
| 327 | |
|---|
| 328 | FD_ZERO(&fds); |
|---|
| 329 | FD_SET(server->serverSock, &fds); |
|---|
| 330 | result = 0; |
|---|
| 331 | while(result == 0) |
|---|
| 332 | { |
|---|
| 333 | result = select(server->serverSock + 1, &fds, 0, 0, timeout); |
|---|
| 334 | if (result < 0) |
|---|
| 335 | { |
|---|
| 336 | return(-1); |
|---|
| 337 | } |
|---|
| 338 | if (timeout != 0 && result == 0) |
|---|
| 339 | { |
|---|
| 340 | return(0); |
|---|
| 341 | } |
|---|
| 342 | if (result > 0) |
|---|
| 343 | { |
|---|
| 344 | break; |
|---|
| 345 | } |
|---|
| 346 | } |
|---|
| 347 | bzero(&addr, sizeof(addr)); |
|---|
| 348 | addrLen = sizeof(addr); |
|---|
| 349 | server->clientSock = accept(server->serverSock,(struct sockaddr *)&addr, |
|---|
| 350 | &addrLen); |
|---|
| 351 | ipaddr = inet_ntoa(addr.sin_addr); |
|---|
| 352 | if (ipaddr) |
|---|
| 353 | strncpy(server->clientAddr, ipaddr, HTTP_IP_ADDR_LEN); |
|---|
| 354 | else |
|---|
| 355 | *server->clientAddr = 0; |
|---|
| 356 | server->readBufRemain = 0; |
|---|
| 357 | server->readBufPtr = NULL; |
|---|
| 358 | |
|---|
| 359 | /* |
|---|
| 360 | ** Check the default ACL |
|---|
| 361 | */ |
|---|
| 362 | if (server->defaultAcl) |
|---|
| 363 | { |
|---|
| 364 | if (httpdCheckAcl(server, server->defaultAcl) == HTTP_ACL_DENY) |
|---|
| 365 | { |
|---|
| 366 | httpdEndRequest(server); |
|---|
| 367 | return(-2); |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | return(1); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | int httpdReadRequest(server) |
|---|
| 376 | httpd *server; |
|---|
| 377 | { |
|---|
| 378 | static char buf[HTTP_MAX_LEN]; |
|---|
| 379 | int count, |
|---|
| 380 | inHeaders; |
|---|
| 381 | char *cp, *cp2; |
|---|
| 382 | int _httpd_decode(); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | /* |
|---|
| 386 | ** Setup for a standard response |
|---|
| 387 | */ |
|---|
| 388 | strcpy(server->response.headers, |
|---|
| 389 | "Server: Hughes Technologies Embedded Server\n"); |
|---|
| 390 | strcpy(server->response.contentType, "text/html"); |
|---|
| 391 | strcpy(server->response.response,"200 Output Follows\n"); |
|---|
| 392 | server->response.headersSent = 0; |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | /* |
|---|
| 396 | ** Read the request |
|---|
| 397 | */ |
|---|
| 398 | count = 0; |
|---|
| 399 | inHeaders = 1; |
|---|
| 400 | while(_httpd_readLine(server, buf, HTTP_MAX_LEN) > 0) |
|---|
| 401 | { |
|---|
| 402 | count++; |
|---|
| 403 | |
|---|
| 404 | /* |
|---|
| 405 | ** Special case for the first line. Scan the request |
|---|
| 406 | ** method and path etc |
|---|
| 407 | */ |
|---|
| 408 | if (count == 1) |
|---|
| 409 | { |
|---|
| 410 | /* |
|---|
| 411 | ** First line. Scan the request info |
|---|
| 412 | */ |
|---|
| 413 | cp = cp2 = buf; |
|---|
| 414 | while(isalpha(*cp2)) |
|---|
| 415 | cp2++; |
|---|
| 416 | *cp2 = 0; |
|---|
| 417 | if (strcasecmp(cp,"GET") == 0) |
|---|
| 418 | server->request.method = HTTP_GET; |
|---|
| 419 | if (strcasecmp(cp,"POST") == 0) |
|---|
| 420 | server->request.method = HTTP_POST; |
|---|
| 421 | if (server->request.method == 0) |
|---|
| 422 | { |
|---|
| 423 | _httpd_net_write( server->clientSock, |
|---|
| 424 | HTTP_METHOD_ERROR, |
|---|
| 425 | strlen(HTTP_METHOD_ERROR)); |
|---|
| 426 | _httpd_net_write( server->clientSock, cp, |
|---|
| 427 | strlen(cp)); |
|---|
| 428 | _httpd_writeErrorLog(server,LEVEL_ERROR, |
|---|
| 429 | "Invalid method received"); |
|---|
| 430 | return(-1); |
|---|
| 431 | } |
|---|
| 432 | cp = cp2+1; |
|---|
| 433 | while(*cp == ' ') |
|---|
| 434 | cp++; |
|---|
| 435 | cp2 = cp; |
|---|
| 436 | while(*cp2 != ' ' && *cp2 != 0) |
|---|
| 437 | cp2++; |
|---|
| 438 | *cp2 = 0; |
|---|
| 439 | strncpy(server->request.path,cp,HTTP_MAX_URL); |
|---|
| 440 | _httpd_sanitiseUrl(server->request.path); |
|---|
| 441 | continue; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | /* |
|---|
| 445 | ** Process the headers |
|---|
| 446 | */ |
|---|
| 447 | if (inHeaders) |
|---|
| 448 | { |
|---|
| 449 | if (*buf == 0) |
|---|
| 450 | { |
|---|
| 451 | /* |
|---|
| 452 | ** End of headers. Continue if there's |
|---|
| 453 | ** data to read |
|---|
| 454 | */ |
|---|
| 455 | if (server->request.contentLength == 0) |
|---|
| 456 | break; |
|---|
| 457 | inHeaders = 0; |
|---|
| 458 | break; |
|---|
| 459 | } |
|---|
| 460 | /** |
|---|
| 461 | * Philippe commenting this out, it crashed with a |
|---|
| 462 | * particular pattern sent from the browser |
|---|
| 463 | * and we don't need it |
|---|
| 464 | if (strncasecmp(buf,"Cookie: ",7) == 0) |
|---|
| 465 | { |
|---|
| 466 | char *var, |
|---|
| 467 | *val, |
|---|
| 468 | *end; |
|---|
| 469 | |
|---|
| 470 | var = index(buf,':'); |
|---|
| 471 | while(var) |
|---|
| 472 | { |
|---|
| 473 | var++; |
|---|
| 474 | val = index(var, '='); |
|---|
| 475 | *val = 0; |
|---|
| 476 | val++; |
|---|
| 477 | end = index(val,';'); |
|---|
| 478 | if(end) |
|---|
| 479 | *end = 0; |
|---|
| 480 | httpdAddVariable(server, var, val); |
|---|
| 481 | var = end; |
|---|
| 482 | } |
|---|
| 483 | } |
|---|
| 484 | */ |
|---|
| 485 | if (strncasecmp(buf,"Authorization: ",15) == 0) |
|---|
| 486 | { |
|---|
| 487 | cp = index(buf,':') + 2; |
|---|
| 488 | if (strncmp(cp,"Basic ", 6) != 0) |
|---|
| 489 | { |
|---|
| 490 | /* Unknown auth method */ |
|---|
| 491 | } |
|---|
| 492 | else |
|---|
| 493 | { |
|---|
| 494 | char authBuf[100]; |
|---|
| 495 | |
|---|
| 496 | cp = index(cp,' ') + 1; |
|---|
| 497 | _httpd_decode(cp, authBuf, 100); |
|---|
| 498 | server->request.authLength = |
|---|
| 499 | strlen(authBuf); |
|---|
| 500 | cp = index(authBuf,':'); |
|---|
| 501 | if (cp) |
|---|
| 502 | { |
|---|
| 503 | *cp = 0; |
|---|
| 504 | strncpy( |
|---|
| 505 | server->request.authPassword, |
|---|
| 506 | cp+1, HTTP_MAX_AUTH); |
|---|
| 507 | } |
|---|
| 508 | strncpy(server->request.authUser, |
|---|
| 509 | authBuf, HTTP_MAX_AUTH); |
|---|
| 510 | } |
|---|
| 511 | } |
|---|
| 512 | if (strncasecmp(buf,"Referer: ",9) == 0) |
|---|
| 513 | { |
|---|
| 514 | cp = index(buf,':') + 2; |
|---|
| 515 | if(cp) |
|---|
| 516 | { |
|---|
| 517 | strncpy(server->request.referer,cp, |
|---|
| 518 | HTTP_MAX_URL); |
|---|
| 519 | } |
|---|
| 520 | } |
|---|
| 521 | /* acv@acv.ca/wifidog: Added decoding of host: if |
|---|
| 522 | * present. */ |
|---|
| 523 | if (strncasecmp(buf,"Host: ",6) == 0) |
|---|
| 524 | { |
|---|
| 525 | cp = index(buf,':') + 2; |
|---|
| 526 | if(cp) |
|---|
| 527 | { |
|---|
| 528 | strncpy(server->request.host,cp, |
|---|
| 529 | HTTP_MAX_URL); |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | /* End modification */ |
|---|
| 533 | if (strncasecmp(buf,"If-Modified-Since: ",19) == 0) |
|---|
| 534 | { |
|---|
| 535 | cp = index(buf,':') + 2; |
|---|
| 536 | if(cp) |
|---|
| 537 | { |
|---|
| 538 | strncpy(server->request.ifModified,cp, |
|---|
| 539 | HTTP_MAX_URL); |
|---|
| 540 | cp = index(server->request.ifModified, |
|---|
| 541 | ';'); |
|---|
| 542 | if (cp) |
|---|
| 543 | *cp = 0; |
|---|
| 544 | } |
|---|
| 545 | } |
|---|
| 546 | if (strncasecmp(buf,"Content-Type: ",14) == 0) |
|---|
| 547 | { |
|---|
| 548 | cp = index(buf,':') + 2; |
|---|
| 549 | if(cp) |
|---|
| 550 | { |
|---|
| 551 | strncpy(server->request.contentType,cp, |
|---|
| 552 | HTTP_MAX_URL); |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | if (strncasecmp(buf,"Content-Length: ",16) == 0) |
|---|
| 556 | { |
|---|
| 557 | cp = index(buf,':') + 2; |
|---|
| 558 | if(cp) |
|---|
| 559 | server->request.contentLength=atoi(cp); |
|---|
| 560 | } |
|---|
| 561 | continue; |
|---|
| 562 | } |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | /* |
|---|
| 566 | ** Process and POST data |
|---|
| 567 | */ |
|---|
| 568 | if (server->request.contentLength > 0) |
|---|
| 569 | { |
|---|
| 570 | bzero(buf, HTTP_MAX_LEN); |
|---|
| 571 | _httpd_readBuf(server, buf, server->request.contentLength); |
|---|
| 572 | _httpd_storeData(server, buf); |
|---|
| 573 | |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | /* |
|---|
| 577 | ** Process any URL data |
|---|
| 578 | */ |
|---|
| 579 | cp = index(server->request.path,'?'); |
|---|
| 580 | if (cp != NULL) |
|---|
| 581 | { |
|---|
| 582 | *cp = 0; |
|---|
| 583 | cp++; |
|---|
| 584 | _httpd_storeData(server, cp); |
|---|
| 585 | } |
|---|
| 586 | return(0); |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | void httpdEndRequest(server) |
|---|
| 591 | httpd *server; |
|---|
| 592 | { |
|---|
| 593 | _httpd_freeVariables(server->variables); |
|---|
| 594 | server->variables = NULL; |
|---|
| 595 | shutdown(server->clientSock,2); |
|---|
| 596 | close(server->clientSock); |
|---|
| 597 | bzero(&server->request, sizeof(server->request)); |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | void httpdFreeVariables(server) |
|---|
| 602 | httpd *server; |
|---|
| 603 | { |
|---|
| 604 | _httpd_freeVariables(server->variables); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | void httpdDumpVariables(server) |
|---|
| 610 | httpd *server; |
|---|
| 611 | { |
|---|
| 612 | httpVar *curVar, |
|---|
| 613 | *curVal; |
|---|
| 614 | |
|---|
| 615 | curVar = server->variables; |
|---|
| 616 | while(curVar) |
|---|
| 617 | { |
|---|
| 618 | printf("Variable '%s'\n", curVar->name); |
|---|
| 619 | curVal = curVar; |
|---|
| 620 | while(curVal) |
|---|
| 621 | { |
|---|
| 622 | printf("\t= '%s'\n",curVal->value); |
|---|
| 623 | curVal = curVal->nextValue; |
|---|
| 624 | } |
|---|
| 625 | curVar = curVar->nextVariable; |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | void httpdSetFileBase(server, path) |
|---|
| 630 | httpd *server; |
|---|
| 631 | char *path; |
|---|
| 632 | { |
|---|
| 633 | strncpy(server->fileBasePath, path, HTTP_MAX_URL); |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | int httpdAddFileContent(server, dir, name, indexFlag, preload, path) |
|---|
| 638 | httpd *server; |
|---|
| 639 | char *dir, |
|---|
| 640 | *name; |
|---|
| 641 | int (*preload)(); |
|---|
| 642 | int indexFlag; |
|---|
| 643 | char *path; |
|---|
| 644 | { |
|---|
| 645 | httpDir *dirPtr; |
|---|
| 646 | httpContent *newEntry; |
|---|
| 647 | |
|---|
| 648 | dirPtr = _httpd_findContentDir(server, dir, HTTP_TRUE); |
|---|
| 649 | newEntry = malloc(sizeof(httpContent)); |
|---|
| 650 | if (newEntry == NULL) |
|---|
| 651 | return(-1); |
|---|
| 652 | bzero(newEntry,sizeof(httpContent)); |
|---|
| 653 | newEntry->name = strdup(name); |
|---|
| 654 | newEntry->type = HTTP_FILE; |
|---|
| 655 | newEntry->indexFlag = indexFlag; |
|---|
| 656 | newEntry->preload = preload; |
|---|
| 657 | newEntry->next = dirPtr->entries; |
|---|
| 658 | dirPtr->entries = newEntry; |
|---|
| 659 | if (*path == '/') |
|---|
| 660 | { |
|---|
| 661 | /* Absolute path */ |
|---|
| 662 | newEntry->path = strdup(path); |
|---|
| 663 | } |
|---|
| 664 | else |
|---|
| 665 | { |
|---|
| 666 | /* Path relative to base path */ |
|---|
| 667 | newEntry->path = malloc(strlen(server->fileBasePath) + |
|---|
| 668 | strlen(path) + 2); |
|---|
| 669 | snprintf(newEntry->path, HTTP_MAX_URL, "%s/%s", |
|---|
| 670 | server->fileBasePath, path); |
|---|
| 671 | } |
|---|
| 672 | return(0); |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | int httpdAddWildcardContent(server, dir, preload, path) |
|---|
| 678 | httpd *server; |
|---|
| 679 | char *dir; |
|---|
| 680 | int (*preload)(); |
|---|
| 681 | char *path; |
|---|
| 682 | { |
|---|
| 683 | httpDir *dirPtr; |
|---|
| 684 | httpContent *newEntry; |
|---|
| 685 | |
|---|
| 686 | dirPtr = _httpd_findContentDir(server, dir, HTTP_TRUE); |
|---|
| 687 | newEntry = malloc(sizeof(httpContent)); |
|---|
| 688 | if (newEntry == NULL) |
|---|
| 689 | return(-1); |
|---|
| 690 | bzero(newEntry,sizeof(httpContent)); |
|---|
| 691 | newEntry->name = NULL; |
|---|
| 692 | newEntry->type = HTTP_WILDCARD; |
|---|
| 693 | newEntry->indexFlag = HTTP_FALSE; |
|---|
| 694 | newEntry->preload = preload; |
|---|
| 695 | newEntry->next = dirPtr->entries; |
|---|
| 696 | dirPtr->entries = newEntry; |
|---|
| 697 | if (*path == '/') |
|---|
| 698 | { |
|---|
| 699 | /* Absolute path */ |
|---|
| 700 | newEntry->path = strdup(path); |
|---|
| 701 | } |
|---|
| 702 | else |
|---|
| 703 | { |
|---|
| 704 | /* Path relative to base path */ |
|---|
| 705 | newEntry->path = malloc(strlen(server->fileBasePath) + |
|---|
| 706 | strlen(path) + 2); |
|---|
| 707 | snprintf(newEntry->path, HTTP_MAX_URL, "%s/%s", |
|---|
| 708 | server->fileBasePath, path); |
|---|
| 709 | } |
|---|
| 710 | return(0); |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | int httpdAddC404Content(server, function) |
|---|
| 717 | httpd *server; |
|---|
| 718 | void (*function)(); |
|---|
| 719 | { |
|---|
| 720 | if (!server->handle404) { |
|---|
| 721 | server->handle404 = (http404*)malloc(sizeof(http404)); |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | if (!server->handle404) { |
|---|
| 725 | return(-1); |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | server->handle404->function = function; |
|---|
| 729 | return(0); |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | int httpdAddCContent(server, dir, name, indexFlag, preload, function) |
|---|
| 733 | httpd *server; |
|---|
| 734 | char *dir; |
|---|
| 735 | char *name; |
|---|
| 736 | int (*preload)(); |
|---|
| 737 | void (*function)(); |
|---|
| 738 | { |
|---|
| 739 | httpDir *dirPtr; |
|---|
| 740 | httpContent *newEntry; |
|---|
| 741 | |
|---|
| 742 | dirPtr = _httpd_findContentDir(server, dir, HTTP_TRUE); |
|---|
| 743 | newEntry = malloc(sizeof(httpContent)); |
|---|
| 744 | if (newEntry == NULL) |
|---|
| 745 | return(-1); |
|---|
| 746 | bzero(newEntry,sizeof(httpContent)); |
|---|
| 747 | newEntry->name = strdup(name); |
|---|
| 748 | newEntry->type = HTTP_C_FUNCT; |
|---|
| 749 | newEntry->indexFlag = indexFlag; |
|---|
| 750 | newEntry->function = function; |
|---|
| 751 | newEntry->preload = preload; |
|---|
| 752 | newEntry->next = dirPtr->entries; |
|---|
| 753 | dirPtr->entries = newEntry; |
|---|
| 754 | return(0); |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | int httpdAddCWildcardContent(server, dir, preload, function) |
|---|
| 759 | httpd *server; |
|---|
| 760 | char *dir; |
|---|
| 761 | int (*preload)(); |
|---|
| 762 | void (*function)(); |
|---|
| 763 | { |
|---|
| 764 | httpDir *dirPtr; |
|---|
| 765 | httpContent *newEntry; |
|---|
| 766 | |
|---|
| 767 | dirPtr = _httpd_findContentDir(server, dir, HTTP_TRUE); |
|---|
| 768 | newEntry = malloc(sizeof(httpContent)); |
|---|
| 769 | if (newEntry == NULL) |
|---|
| 770 | return(-1); |
|---|
| 771 | bzero(newEntry,sizeof(httpContent)); |
|---|
| 772 | newEntry->name = NULL; |
|---|
| 773 | newEntry->type = HTTP_C_WILDCARD; |
|---|
| 774 | newEntry->indexFlag = HTTP_FALSE; |
|---|
| 775 | newEntry->function = function; |
|---|
| 776 | newEntry->preload = preload; |
|---|
| 777 | newEntry->next = dirPtr->entries; |
|---|
| 778 | dirPtr->entries = newEntry; |
|---|
| 779 | return(0); |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | int httpdAddStaticContent(server, dir, name, indexFlag, preload, data) |
|---|
| 783 | httpd *server; |
|---|
| 784 | char *dir; |
|---|
| 785 | char *name; |
|---|
| 786 | int (*preload)(); |
|---|
| 787 | char *data; |
|---|
| 788 | { |
|---|
| 789 | httpDir *dirPtr; |
|---|
| 790 | httpContent *newEntry; |
|---|
| 791 | |
|---|
| 792 | dirPtr = _httpd_findContentDir(server, dir, HTTP_TRUE); |
|---|
| 793 | newEntry = malloc(sizeof(httpContent)); |
|---|
| 794 | if (newEntry == NULL) |
|---|
| 795 | return(-1); |
|---|
| 796 | bzero(newEntry,sizeof(httpContent)); |
|---|
| 797 | newEntry->name = strdup(name); |
|---|
| 798 | newEntry->type = HTTP_STATIC; |
|---|
| 799 | newEntry->indexFlag = indexFlag; |
|---|
| 800 | newEntry->data = data; |
|---|
| 801 | newEntry->preload = preload; |
|---|
| 802 | newEntry->next = dirPtr->entries; |
|---|
| 803 | dirPtr->entries = newEntry; |
|---|
| 804 | return(0); |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | void httpdSendHeaders(server) |
|---|
| 808 | httpd *server; |
|---|
| 809 | { |
|---|
| 810 | _httpd_sendHeaders(server, 0, 0); |
|---|
| 811 | } |
|---|
| 812 | |
|---|
| 813 | void httpdSetResponse(server, msg) |
|---|
| 814 | httpd *server; |
|---|
| 815 | char *msg; |
|---|
| 816 | { |
|---|
| 817 | strncpy(server->response.response, msg, HTTP_MAX_URL); |
|---|
| 818 | } |
|---|
| 819 | |
|---|
| 820 | void httpdSetContentType(server, type) |
|---|
| 821 | httpd *server; |
|---|
| 822 | char *type; |
|---|
| 823 | { |
|---|
| 824 | strcpy(server->response.contentType, type); |
|---|
| 825 | } |
|---|
| 826 | |
|---|
| 827 | |
|---|
| 828 | void httpdAddHeader(server, msg) |
|---|
| 829 | httpd *server; |
|---|
| 830 | char *msg; |
|---|
| 831 | { |
|---|
| 832 | strcat(server->response.headers,msg); |
|---|
| 833 | if (msg[strlen(msg) - 1] != '\n') |
|---|
| 834 | strcat(server->response.headers,"\n"); |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | void httpdSetCookie(server, name, value) |
|---|
| 838 | httpd *server; |
|---|
| 839 | char *name, |
|---|
| 840 | *value; |
|---|
| 841 | { |
|---|
| 842 | char buf[HTTP_MAX_URL]; |
|---|
| 843 | |
|---|
| 844 | snprintf(buf,HTTP_MAX_URL, "Set-Cookie: %s=%s; path=/;", name, value); |
|---|
| 845 | httpdAddHeader(server,buf); |
|---|
| 846 | } |
|---|
| 847 | |
|---|
| 848 | void httpdOutput(server, msg) |
|---|
| 849 | httpd *server; |
|---|
| 850 | char *msg; |
|---|
| 851 | { |
|---|
| 852 | char buf[HTTP_MAX_LEN], |
|---|
| 853 | varName[80], |
|---|
| 854 | *src, |
|---|
| 855 | *dest; |
|---|
| 856 | int count; |
|---|
| 857 | |
|---|
| 858 | src = msg; |
|---|
| 859 | dest = buf; |
|---|
| 860 | count = 0; |
|---|
| 861 | while(*src && count < HTTP_MAX_LEN) |
|---|
| 862 | { |
|---|
| 863 | if (*src == '$') |
|---|
| 864 | { |
|---|
| 865 | char *cp, |
|---|
| 866 | *tmp; |
|---|
| 867 | int count2; |
|---|
| 868 | httpVar *curVar; |
|---|
| 869 | |
|---|
| 870 | tmp = src + 1; |
|---|
| 871 | cp = varName; |
|---|
| 872 | count2 = 0; |
|---|
| 873 | while(*tmp&&(isalnum(*tmp)||*tmp == '_')&&count2 < 80) |
|---|
| 874 | { |
|---|
| 875 | *cp++ = *tmp++; |
|---|
| 876 | count2++; |
|---|
| 877 | } |
|---|
| 878 | *cp = 0; |
|---|
| 879 | curVar = httpdGetVariableByName(server,varName); |
|---|
| 880 | if (curVar) |
|---|
| 881 | { |
|---|
| 882 | strcpy(dest, curVar->value); |
|---|
| 883 | dest = dest + strlen(dest); |
|---|
| 884 | count += strlen(dest); |
|---|
| 885 | } |
|---|
| 886 | else |
|---|
| 887 | { |
|---|
| 888 | *dest++ = '$'; |
|---|
| 889 | strcpy(dest, varName); |
|---|
| 890 | dest += strlen(varName); |
|---|
| 891 | count += 1 + strlen(varName); |
|---|
| 892 | } |
|---|
| 893 | src = src + strlen(varName) + 1; |
|---|
| 894 | continue; |
|---|
| 895 | } |
|---|
| 896 | *dest++ = *src++; |
|---|
| 897 | count++; |
|---|
| 898 | } |
|---|
| 899 | *dest = 0; |
|---|
| 900 | server->response.responseLength += strlen(buf); |
|---|
| 901 | if (server->response.headersSent == 0) |
|---|
| 902 | httpdSendHeaders(server); |
|---|
| 903 | _httpd_net_write( server->clientSock, buf, strlen(buf)); |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | |
|---|
| 907 | |
|---|
| 908 | #ifdef HAVE_STDARG_H |
|---|
| 909 | void httpdPrintf(httpd *server, char *fmt, ...) |
|---|
| 910 | { |
|---|
| 911 | #else |
|---|
| 912 | void httpdPrintf(va_alist) |
|---|
| 913 | va_dcl |
|---|
| 914 | { |
|---|
| 915 | httpd *server; |
|---|
| 916 | char *fmt; |
|---|
| 917 | #endif |
|---|
| 918 | va_list args; |
|---|
| 919 | char buf[HTTP_MAX_LEN]; |
|---|
| 920 | |
|---|
| 921 | #ifdef HAVE_STDARG_H |
|---|
| 922 | va_start(args, fmt); |
|---|
| 923 | #else |
|---|
| 924 | va_start(args); |
|---|
| 925 | server = (httpd *) va_arg(args, httpd * ); |
|---|
| 926 | fmt = (char *) va_arg(args, char *); |
|---|
| 927 | #endif |
|---|
| 928 | if (server->response.headersSent == 0) |
|---|
| 929 | httpdSendHeaders(server); |
|---|
| 930 | vsnprintf(buf, HTTP_MAX_LEN, fmt, args); |
|---|
| 931 | server->response.responseLength += strlen(buf); |
|---|
| 932 | _httpd_net_write( server->clientSock, buf, strlen(buf)); |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| 935 | |
|---|
| 936 | |
|---|
| 937 | |
|---|
| 938 | void httpdProcessRequest(server) |
|---|
| 939 | httpd *server; |
|---|
| 940 | { |
|---|
| 941 | char dirName[HTTP_MAX_URL], |
|---|
| 942 | entryName[HTTP_MAX_URL], |
|---|
| 943 | *cp; |
|---|
| 944 | httpDir *dir; |
|---|
| 945 | httpContent *entry; |
|---|
| 946 | |
|---|
| 947 | server->response.responseLength = 0; |
|---|
| 948 | strncpy(dirName, httpdRequestPath(server), HTTP_MAX_URL); |
|---|
| 949 | cp = rindex(dirName, '/'); |
|---|
| 950 | if (cp == NULL) |
|---|
| 951 | { |
|---|
| 952 | printf("Invalid request path '%s'\n",dirName); |
|---|
| 953 | return; |
|---|
| 954 | } |
|---|
| 955 | strncpy(entryName, cp + 1, HTTP_MAX_URL); |
|---|
| 956 | if (cp != dirName) |
|---|
| 957 | *cp = 0; |
|---|
| 958 | else |
|---|
| 959 | *(cp+1) = 0; |
|---|
| 960 | dir = _httpd_findContentDir(server, dirName, HTTP_FALSE); |
|---|
| 961 | if (dir == NULL) |
|---|
| 962 | { |
|---|
| 963 | _httpd_send404(server); |
|---|
| 964 | _httpd_writeAccessLog(server); |
|---|
| 965 | return; |
|---|
| 966 | } |
|---|
| 967 | entry = _httpd_findContentEntry(server, dir, entryName); |
|---|
| 968 | if (entry == NULL) |
|---|
| 969 | { |
|---|
| 970 | _httpd_send404(server); |
|---|
| 971 | _httpd_writeAccessLog(server); |
|---|
| 972 | return; |
|---|
| 973 | } |
|---|
| 974 | if (entry->preload) |
|---|
| 975 | { |
|---|
| 976 | if ((entry->preload)(server) < 0) |
|---|
| 977 | { |
|---|
| 978 | _httpd_writeAccessLog(server); |
|---|
| 979 | return; |
|---|
| 980 | } |
|---|
| 981 | } |
|---|
| 982 | switch(entry->type) |
|---|
| 983 | { |
|---|
| 984 | case HTTP_C_FUNCT: |
|---|
| 985 | case HTTP_C_WILDCARD: |
|---|
| 986 | (entry->function)(server); |
|---|
| 987 | break; |
|---|
| 988 | |
|---|
| 989 | case HTTP_STATIC: |
|---|
| 990 | _httpd_sendStatic(server, entry->data); |
|---|
| 991 | break; |
|---|
| 992 | |
|---|
| 993 | case HTTP_FILE: |
|---|
| 994 | _httpd_sendFile(server, entry->path); |
|---|
| 995 | break; |
|---|
| 996 | |
|---|
| 997 | case HTTP_WILDCARD: |
|---|
| 998 | if (_httpd_sendDirectoryEntry(server,entry,entryName)<0) |
|---|
| 999 | { |
|---|
| 1000 | _httpd_send404(server); |
|---|
| 1001 | } |
|---|
| 1002 | break; |
|---|
| 1003 | } |
|---|
| 1004 | _httpd_writeAccessLog(server); |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| 1007 | void httpdSetAccessLog(server, fp) |
|---|
| 1008 | httpd *server; |
|---|
| 1009 | FILE *fp; |
|---|
| 1010 | { |
|---|
| 1011 | server->accessLog = fp; |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | void httpdSetErrorLog(server, fp) |
|---|
| 1015 | httpd *server; |
|---|
| 1016 | FILE *fp; |
|---|
| 1017 | { |
|---|
| 1018 | server->errorLog = fp; |
|---|
| 1019 | } |
|---|
| 1020 | |
|---|
| 1021 | void httpdAuthenticate(server, realm) |
|---|
| 1022 | httpd *server; |
|---|
| 1023 | char *realm; |
|---|
| 1024 | { |
|---|
| 1025 | char buffer[255]; |
|---|
| 1026 | |
|---|
| 1027 | if (server->request.authLength == 0) |
|---|
| 1028 | { |
|---|
| 1029 | httpdSetResponse(server, "401 Please Authenticate"); |
|---|
| 1030 | snprintf(buffer,sizeof(buffer), |
|---|
| 1031 | "WWW-Authenticate: Basic realm=\"%s\"\n", realm); |
|---|
| 1032 | httpdAddHeader(server, buffer); |
|---|
| 1033 | httpdOutput(server,"\n"); |
|---|
| 1034 | } |
|---|
| 1035 | } |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | void httpdForceAuthenticate(server, realm) |
|---|
| 1039 | httpd *server; |
|---|
| 1040 | char *realm; |
|---|
| 1041 | { |
|---|
| 1042 | char buffer[255]; |
|---|
| 1043 | |
|---|
| 1044 | httpdSetResponse(server, "401 Please Authenticate"); |
|---|
| 1045 | snprintf(buffer,sizeof(buffer), |
|---|
| 1046 | "WWW-Authenticate: Basic realm=\"%s\"\n", realm); |
|---|
| 1047 | httpdAddHeader(server, buffer); |
|---|
| 1048 | httpdOutput(server,"\n"); |
|---|
| 1049 | } |
|---|