ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/thttpd/libhttpd.c
(Generate patch)

Comparing thttpd/libhttpd.c (file contents):
Revision 1.1.4.2 by root, Fri Jun 29 13:08:08 2001 UTC vs.
Revision 1.3 by root, Mon Jul 2 18:38:31 2001 UTC

155static char** make_envp( httpd_conn* hc ); 155static char** make_envp( httpd_conn* hc );
156static char** make_argp( httpd_conn* hc ); 156static char** make_argp( httpd_conn* hc );
157static void cgi_interpose_input( httpd_conn* hc, int wfd ); 157static void cgi_interpose_input( httpd_conn* hc, int wfd );
158static void post_post_garbage_hack( httpd_conn* hc ); 158static void post_post_garbage_hack( httpd_conn* hc );
159static void cgi_interpose_output( httpd_conn* hc, int rfd ); 159static void cgi_interpose_output( httpd_conn* hc, int rfd );
160static void cgi_child( httpd_conn* hc ); 160static void cgi_child( httpd_conn* hc, char* exefilename );
161static off_t cgi( httpd_conn* hc ); 161static off_t cgi( httpd_conn* hc, char* exefilename );
162static int really_start_request( httpd_conn* hc, struct timeval* nowP ); 162static int really_start_request( httpd_conn* hc, struct timeval* nowP );
163static void make_log_entry( httpd_conn* hc, struct timeval* nowP ); 163static void make_log_entry( httpd_conn* hc, struct timeval* nowP );
164static int check_referer( httpd_conn* hc ); 164static int check_referer( httpd_conn* hc );
165static int really_check_referer( httpd_conn* hc ); 165static int really_check_referer( httpd_conn* hc );
166static int sockaddr_check( httpd_sockaddr* saP ); 166static int sockaddr_check( httpd_sockaddr* saP );
238 { 238 {
239 if ( hs->binding_hostname != (char*) 0 ) 239 if ( hs->binding_hostname != (char*) 0 )
240 free( (void*) hs->binding_hostname ); 240 free( (void*) hs->binding_hostname );
241 if ( hs->cwd != (char*) 0 ) 241 if ( hs->cwd != (char*) 0 )
242 free( (void*) hs->cwd ); 242 free( (void*) hs->cwd );
243 if ( hs->autoindex_prog != (char*) 0 )
244 free( (void*) hs->autoindex_prog );
243 if ( hs->cgi_pattern != (char*) 0 ) 245 if ( hs->cgi_pattern != (char*) 0 )
244 free( (void*) hs->cgi_pattern ); 246 free( (void*) hs->cgi_pattern );
245 if ( hs->charset != (char*) 0 ) 247 if ( hs->charset != (char*) 0 )
246 free( (void*) hs->charset ); 248 free( (void*) hs->charset );
247 if ( hs->url_pattern != (char*) 0 ) 249 if ( hs->url_pattern != (char*) 0 )
255httpd_server* 257httpd_server*
256httpd_initialize( 258httpd_initialize(
257 char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port, 259 char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port,
258 char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp, 260 char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp,
259 int no_symlink, int vhost, int global_passwd, char* url_pattern, 261 int no_symlink, int vhost, int global_passwd, char* url_pattern,
260 char* local_pattern, int no_empty_referers ) 262 char* local_pattern, int no_empty_referers,
263 char* autoindex_prog)
261 { 264 {
262 httpd_server* hs; 265 httpd_server* hs;
263 static char ghnbuf[256]; 266 static char ghnbuf[256];
264 char* cp; 267 char* cp;
265 268
327 } 330 }
328 /* Nuke any leading slashes in the cgi pattern. */ 331 /* Nuke any leading slashes in the cgi pattern. */
329 while ( ( cp = strstr( hs->cgi_pattern, "|/" ) ) != (char*) 0 ) 332 while ( ( cp = strstr( hs->cgi_pattern, "|/" ) ) != (char*) 0 )
330 (void) strcpy( cp + 1, cp + 2 ); 333 (void) strcpy( cp + 1, cp + 2 );
331 } 334 }
335 hs->autoindex_prog = autoindex_prog ? strdup( autoindex_prog ) : 0;
332 hs->charset = strdup( charset ); 336 hs->charset = strdup( charset );
333 hs->cwd = strdup( cwd ); 337 hs->cwd = strdup( cwd );
334 if ( hs->cwd == (char*) 0 ) 338 if ( hs->cwd == (char*) 0 )
335 { 339 {
336 syslog( LOG_CRIT, "out of memory copying cwd" ); 340 syslog( LOG_CRIT, "out of memory copying cwd" );
1465 checked[0] = '\0'; 1469 checked[0] = '\0';
1466 checkedlen = 0; 1470 checkedlen = 0;
1467 restlen = strlen( path ); 1471 restlen = strlen( path );
1468 httpd_realloc_str( &rest, &maxrest, restlen ); 1472 httpd_realloc_str( &rest, &maxrest, restlen );
1469 (void) strcpy( rest, path ); 1473 (void) strcpy( rest, path );
1470 if ( rest[restlen - 1] == '/' )
1471 rest[--restlen] = '\0'; /* trim trailing slash */
1472 if ( ! tildemapped ) 1474 if ( ! tildemapped )
1473 /* Remove any leading slashes. */ 1475 /* Remove any leading slashes. */
1474 while ( rest[0] == '/' ) 1476 while ( rest[0] == '/' )
1475 { 1477 {
1476 (void) strcpy( rest, &(rest[1]) ); 1478 (void) strcpy( rest, &(rest[1]) );
1732 hc->init_byte_loc = 0; 1734 hc->init_byte_loc = 0;
1733 hc->end_byte_loc = -1; 1735 hc->end_byte_loc = -1;
1734 hc->keep_alive = 0; 1736 hc->keep_alive = 0;
1735 hc->should_linger = 0; 1737 hc->should_linger = 0;
1736 hc->file_address = (char*) 0; 1738 hc->file_address = (char*) 0;
1739#ifdef MMAP_MAX
1740 hc->file_fd = -1;
1741 hc->write_buf = (char*) 0;
1742#endif
1737 return GC_OK; 1743 return GC_OK;
1738 } 1744 }
1739 1745
1740 1746
1741/* Checks hc->read_buf to see whether a complete request has been read so far; 1747/* Checks hc->read_buf to see whether a complete request has been read so far;
2382void 2388void
2383httpd_close_conn( httpd_conn* hc, struct timeval* nowP ) 2389httpd_close_conn( httpd_conn* hc, struct timeval* nowP )
2384 { 2390 {
2385 make_log_entry( hc, nowP ); 2391 make_log_entry( hc, nowP );
2386 2392
2393#ifdef MMAP_MAX
2394 if ( hc->file_fd >= 0)
2395 {
2396 (void) close( hc->file_fd );
2397 hc->file_fd = -1;
2398 }
2399 if ( hc->write_buf )
2400 {
2401 (void) free (hc->write_buf);
2402 hc->write_buf = 0;
2403 }
2404#endif
2387 if ( hc->file_address != (char*) 0 ) 2405 if ( hc->file_address != (char*) 0 )
2388 { 2406 {
2389 mmc_unmap( hc->file_address, &(hc->sb), nowP ); 2407 mmc_unmap( hc->file_address, &(hc->sb), nowP );
2390 hc->file_address = (char*) 0; 2408 hc->file_address = (char*) 0;
2391 } 2409 }
3156 } 3174 }
3157 3175
3158 3176
3159/* CGI child process. */ 3177/* CGI child process. */
3160static void 3178static void
3161cgi_child( httpd_conn* hc ) 3179cgi_child( httpd_conn* hc, char* exefilename )
3162 { 3180 {
3163 int r; 3181 int r;
3164 char** argp; 3182 char** argp;
3165 char** envp; 3183 char** envp;
3166 char* binary; 3184 char* binary;
3308 3326
3309 /* Split the program into directory and binary, so we can chdir() 3327 /* Split the program into directory and binary, so we can chdir()
3310 ** to the program's own directory. This isn't in the CGI 1.1 3328 ** to the program's own directory. This isn't in the CGI 1.1
3311 ** spec, but it's what other HTTP servers do. 3329 ** spec, but it's what other HTTP servers do.
3312 */ 3330 */
3313 directory = strdup( hc->expnfilename ); 3331 if (exefilename)
3314 if ( directory == (char*) 0 ) 3332 binary = exefilename;
3315 binary = hc->expnfilename; /* ignore errors */
3316 else 3333 else
3317 { 3334 {
3335 directory = strdup( exefilename ? exefilename : hc->expnfilename );
3336 if ( directory == (char*) 0 )
3337 binary = exefilename; /* ignore errors */
3338 else
3339 {
3318 binary = strrchr( directory, '/' ); 3340 binary = strrchr( directory, '/' );
3319 if ( binary == (char*) 0 ) 3341 if ( binary == (char*) 0 )
3320 binary = hc->expnfilename; 3342 binary = exefilename;
3321 else 3343 else
3322 { 3344 {
3323 *binary++ = '\0'; 3345 *binary++ = '\0';
3324 (void) chdir( directory ); /* ignore errors */ 3346 (void) chdir( directory ); /* ignore errors */
3347 }
3348 }
3325 } 3349 }
3326 }
3327 3350
3328 /* Default behavior for SIGPIPE. */ 3351 /* Default behavior for SIGPIPE. */
3329 (void) signal( SIGPIPE, SIG_DFL ); 3352 (void) signal( SIGPIPE, SIG_DFL );
3330 3353
3331 /* Run the program. */ 3354 /* Run the program. */
3337 exit( 1 ); 3360 exit( 1 );
3338 } 3361 }
3339 3362
3340 3363
3341static off_t 3364static off_t
3342cgi( httpd_conn* hc ) 3365cgi( httpd_conn* hc, char* exefilename )
3343 { 3366 {
3344 int r; 3367 int r;
3345 ClientData client_data; 3368 ClientData client_data;
3346 3369
3347 if ( hc->method == METHOD_GET || hc->method == METHOD_POST ) 3370 if ( hc->method == METHOD_GET || hc->method == METHOD_POST )
3355 return -1; 3378 return -1;
3356 } 3379 }
3357 if ( r == 0 ) 3380 if ( r == 0 )
3358 { 3381 {
3359 unlisten( hc->hs ); 3382 unlisten( hc->hs );
3360 cgi_child( hc ); 3383 cgi_child( hc, exefilename );
3361 } 3384 }
3362 3385
3363 /* Parent process. */ 3386 /* Parent process. */
3364 syslog( LOG_INFO, "spawned CGI process %d for file '%.200s'", r, hc->expnfilename ); 3387 syslog( LOG_INFO, "spawned CGI process %d for file '%.200s'", r, hc->expnfilename );
3365#ifdef CGI_TIMELIMIT 3388#ifdef CGI_TIMELIMIT
3495#endif /* AUTH_FILE */ 3518#endif /* AUTH_FILE */
3496 /* Referer check. */ 3519 /* Referer check. */
3497 if ( ! check_referer( hc ) ) 3520 if ( ! check_referer( hc ) )
3498 return -1; 3521 return -1;
3499 /* Ok, generate an index. */ 3522 /* Ok, generate an index. */
3500 return ls( hc ); 3523 return hc->hs->autoindex_prog ? cgi( hc, hc->hs->autoindex_prog) : ls( hc );
3501#else /* GENERATE_INDEXES */ 3524#else /* GENERATE_INDEXES */
3502 syslog( 3525 syslog(
3503 LOG_INFO, "%.80s URL \"%.80s\" tried to index a directory", 3526 LOG_INFO, "%.80s URL \"%.80s\" tried to index a directory",
3504 httpd_ntoa( &hc->client_addr ), hc->encodedurl ); 3527 httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3505 httpd_send_err( 3528 httpd_send_err(
3588 3611
3589 /* Is it world-executable and in the CGI area? */ 3612 /* Is it world-executable and in the CGI area? */
3590 if ( hc->hs->cgi_pattern != (char*) 0 && 3613 if ( hc->hs->cgi_pattern != (char*) 0 &&
3591 ( hc->sb.st_mode & S_IXOTH ) && 3614 ( hc->sb.st_mode & S_IXOTH ) &&
3592 match( hc->hs->cgi_pattern, hc->expnfilename ) ) 3615 match( hc->hs->cgi_pattern, hc->expnfilename ) )
3593 return cgi( hc ); 3616 return cgi( hc, 0 );
3594 3617
3595 /* It's not CGI. If it's executable or there's pathinfo, someone's 3618 /* It's not CGI. If it's executable or there's pathinfo, someone's
3596 ** trying to either serve or run a non-CGI file as CGI. Either case 3619 ** trying to either serve or run a non-CGI file as CGI. Either case
3597 ** is prohibited. 3620 ** is prohibited.
3598 */ 3621 */
3640 hc, 304, err304title, hc->encodings, "", hc->type, hc->sb.st_size, 3663 hc, 304, err304title, hc->encodings, "", hc->type, hc->sb.st_size,
3641 hc->sb.st_mtime ); 3664 hc->sb.st_mtime );
3642 } 3665 }
3643 else 3666 else
3644 { 3667 {
3668#ifdef MMAP_MAX
3669 if ( hc->sb.st_size < MMAP_MAX)
3670#endif
3645 hc->file_address = mmc_map( hc->expnfilename, &(hc->sb), nowP ); 3671 hc->file_address = mmc_map( hc->expnfilename, &(hc->sb), nowP );
3646 if ( hc->file_address == (char*) 0 ) 3672 if ( hc->file_address == (char*) 0 )
3647 { 3673 {
3674#ifdef MMAP_MAX
3675 hc->file_fd = open ( hc->expnfilename, O_RDONLY);
3676 hc->write_buf = malloc (WRITE_BUFFER);
3677 hc->write_ofs = WRITE_BUFFER;
3678 if ( hc->file_fd < 0 || !hc->write_buf )
3679#endif
3680 {
3648 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); 3681 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3649 return -1; 3682 return -1;
3683 }
3650 } 3684 }
3651 send_mime( 3685 send_mime(
3652 hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size, 3686 hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size,
3653 hc->sb.st_mtime ); 3687 hc->sb.st_mtime );
3654 } 3688 }
3677 char url[305]; 3711 char url[305];
3678 char bytes[40]; 3712 char bytes[40];
3679 3713
3680 if ( hc->hs->no_log ) 3714 if ( hc->hs->no_log )
3681 return; 3715 return;
3716
3717 /* don't log UNKNOWN protocol requests (blocks etc..) */
3718 if ( !strcmp (hc->protocol, "UNKNOWN") )
3719 return;
3682 3720
3683 /* This is straight CERN Combined Log Format - the only tweak 3721 /* This is straight CERN Combined Log Format - the only tweak
3684 ** being that if we're using syslog() we leave out the date, because 3722 ** being that if we're using syslog() we leave out the date, because
3685 ** syslogd puts it in. The included syslogtocern script turns the 3723 ** syslogd puts it in. The included syslogtocern script turns the
3686 ** results into true CERN format. 3724 ** results into true CERN format.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines