Lines Matching refs:hc

128 static void add_response( httpd_conn* hc, char* str );
129 static void send_mime( httpd_conn* hc, int status, char* title, char* encodings, char* extraheads, char* type, off_t length, time_t mod );
130 static void send_response( httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg );
131 static void send_response_tail( httpd_conn* hc );
134 static int send_err_file( httpd_conn* hc, int status, char* title, char* extraheads, char* filename );
137 static void send_authenticate( httpd_conn* hc, char* realm );
139 static int auth_check( httpd_conn* hc, char* dirname );
140 static int auth_check2( httpd_conn* hc, char* dirname );
142 static void send_dirredirect( httpd_conn* hc );
149 static int tilde_map_1( httpd_conn* hc );
152 static int tilde_map_2( httpd_conn* hc );
154 static int vhost_map( httpd_conn* hc );
156 static char* bufgets( httpd_conn* hc );
159 static void figure_mime( httpd_conn* hc );
165 static int ls( httpd_conn* hc );
171 //static char** make_envp( httpd_conn* hc );
172 static char** make_argp( httpd_conn* hc );
173 static void cgi_interpose_input( httpd_conn* hc, int wfd );
174 static void post_post_garbage_hack( httpd_conn* hc );
175 static void cgi_interpose_output( httpd_conn* hc, int rfd );
176 static void cgi_child( httpd_conn* hc );
177 static int cgi( httpd_conn* hc );
178 static int really_start_request( httpd_conn* hc, struct timeval* nowP );
179 static void make_log_entry( httpd_conn* hc, struct timeval* nowP );
180 static int check_referrer( httpd_conn* hc );
181 static int really_check_referrer( httpd_conn* hc );
560 add_response( httpd_conn* hc, char* str )
565 httpd_realloc_str( &hc->response, &hc->maxresponse, hc->responselen + len );
566 (void) memmove( &(hc->response[hc->responselen]), str, len );
567 hc->responselen += len;
572 httpd_write_response( httpd_conn* hc )
576 httpd_clear_ndelay( hc->conn_fd );*/
578 if ( hc->responselen > 0 )
580 (void) httpd_write_fully( hc->conn_fd, hc->response, hc->responselen );
581 hc->responselen = 0;
619 send_mime( httpd_conn* hc, int status, char* title, char* encodings, char* extraheads, char* type, off_t length, time_t mod )
631 hc->status = status;
632 hc->bytes_to_send = length;
633 if ( hc->mime_flag )
635 if ( status == 200 && hc->got_range &&
636 ( hc->last_byte_index >= hc->first_byte_index ) &&
637 ( ( hc->last_byte_index != length - 1 ) ||
638 ( hc->first_byte_index != 0 ) ) &&
639 ( hc->range_if == (time_t) -1 ||
640 hc->range_if == hc->sb.st_mtime ) )
643 hc->status = status = 206;
649 hc->got_range = 0;
658 fixed_type, sizeof(fixed_type), type, hc->hs->charset );
661 hc->protocol, status, title, EXPOSED_SERVER_SOFTWARE, fixed_type,
663 add_response( hc, buf );
669 add_response( hc, buf );
675 add_response( hc, buf );
681 (long long) hc->first_byte_index,
682 (long long) hc->last_byte_index,
684 (long long) ( hc->last_byte_index - hc->first_byte_index + 1 ) );
685 add_response( hc, buf );
691 add_response( hc, buf );
693 if ( hc->hs->p3p[0] != '\0' )
695 (void) my_snprintf( buf, sizeof(buf), "P3P: %s\015\012", hc->hs->p3p );
696 add_response( hc, buf );
698 if ( hc->hs->max_age >= 0 )
700 expires = now + hc->hs->max_age;
705 hc->hs->max_age, expbuf );
706 add_response( hc, buf );
709 add_response( hc, extraheads );
710 add_response( hc, "\015\012" );
748 send_response( httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg )
753 hc, status, title, "", extraheads, "text/html; charset=%s", (off_t) -1,
769 add_response( hc, buf );
772 add_response( hc, buf );
773 if ( match( "**MSIE**", hc->useragent ) )
776 add_response( hc, "<!--\n" );
778 add_response( hc, "Padding so that MSIE deigns to show this error instead of its own canned one.\n");
779 add_response( hc, "-->\n" );
781 send_response_tail( hc );
786 send_response_tail( httpd_conn* hc )
799 add_response( hc, buf );
837 httpd_send_err( httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg )
844 if ( hc->hs->vhost && hc->hostdir[0] != '\0' )
847 "%s/%s/err%d.html", hc->hostdir, ERR_DIR, status );
848 if ( send_err_file( hc, status, title, extraheads, filename ) )
855 if ( send_err_file( hc, status, title, extraheads, filename ) )
859 send_response( hc, status, title, extraheads, form, arg );
863 send_response( hc, status, title, extraheads, form, arg );
871 send_err_file( httpd_conn* hc, int status, char* title, char* extraheads, char* filename )
881 hc, status, title, "", extraheads, "text/html; charset=%s", (off_t) -1,
889 add_response( hc, buf );
894 send_response_tail( hc );
905 send_authenticate( httpd_conn* hc, char* realm )
914 httpd_send_err( hc, 401, err401title, header, err401form, hc->encodedurl );
918 if ( hc->method == METHOD_POST )
919 hc->should_linger = 1;
1006 auth_check( httpd_conn* hc, char* dirname )
1008 if ( hc->hs->global_passwd )
1011 if ( hc->hs->vhost && hc->hostdir[0] != '\0' )
1012 topdir = hc->hostdir;
1015 switch ( auth_check2( hc, topdir ) )
1023 return auth_check2( hc, dirname );
1029 auth_check2( httpd_conn* hc, char* dirname )
1060 if ( hc->authorization[0] == '\0' ||
1061 strncmp( hc->authorization, "Basic ", 6 ) != 0 )
1064 send_authenticate( hc, dirname );
1070 &(hc->authorization[6]), (unsigned char*) authinfo,
1078 send_authenticate( hc, dirname );
1098 &hc->remoteuser, &hc->maxremoteuser, strlen( authinfo ) );
1099 (void) strcpy( hc->remoteuser, authinfo );
1105 send_authenticate( hc, dirname );
1117 // httpd_ntoa( &hc->client_addr ), authpath );
1119 hc, 403, err403title, "",
1121 hc->encodedurl );
1147 &hc->remoteuser, &hc->maxremoteuser, strlen( line ) );
1148 (void) strcpy( hc->remoteuser, line );
1164 send_authenticate( hc, dirname );
1172 send_authenticate( hc, dirname );
1180 send_dirredirect( httpd_conn* hc )
1187 if ( hc->query[0] != '\0')
1189 char* cp = strchr( hc->encodedurl, '?' );
1194 strlen( hc->encodedurl ) + 2 + strlen( hc->query ) );
1196 "%s/?%s", hc->encodedurl, hc->query );
1201 &location, &maxlocation, strlen( hc->encodedurl ) + 1 );
1203 "%s/", hc->encodedurl );
1209 send_response( hc, 302, err302title, header, err302form, location );
1292 tilde_map_1( httpd_conn* hc )
1299 len = strlen( hc->expnfilename ) - 1;
1301 (void) strcpy( temp, &hc->expnfilename[1] );
1303 &hc->expnfilename, &hc->maxexpnfilename, strlen( prefix ) + 1 + len );
1304 (void) strcpy( hc->expnfilename, prefix );
1306 (void) strcat( hc->expnfilename, "/" );
1307 (void) strcat( hc->expnfilename, temp );
1315 tilde_map_2( httpd_conn* hc )
1326 httpd_realloc_str( &temp, &maxtemp, strlen( hc->expnfilename ) - 1 );
1327 (void) strcpy( temp, &hc->expnfilename[1] );
1341 &hc->altdir, &hc->maxaltdir,
1343 (void) strcpy( hc->altdir, pw->pw_dir );
1346 (void) strcat( hc->altdir, "/" );
1347 (void) strcat( hc->altdir, postfix );
1349 alt = expand_symlinks( hc->altdir, &rest, 0, 1 );
1352 httpd_realloc_str( &hc->altdir, &hc->maxaltdir, strlen( alt ) );
1353 (void) strcpy( hc->altdir, alt );
1357 &hc->expnfilename, &hc->maxexpnfilename,
1358 strlen( hc->altdir ) + 1 + strlen( cp ) );
1359 (void) my_snprintf( hc->expnfilename, hc->maxexpnfilename,
1360 "%s/%s", hc->altdir, cp );
1363 hc->tildemapped = 1;
1372 vhost_map( httpd_conn* hc )
1386 if ( hc->reqhost[0] != '\0' )
1387 hc->hostname = hc->reqhost;
1388 else if ( hc->hdrhost[0] != '\0' )
1389 hc->hostname = hc->hdrhost;
1393 if ( getsockname( hc->conn_fd, &sa.sa, &sz ) < 0 )
1398 hc->hostname = httpd_ntoa( &sa );
1401 for ( cp1 = hc->hostname; *cp1 != '\0'; ++cp1 )
1405 if ( hc->tildemapped )
1411 &hc->hostdir, &hc->maxhostdir,
1412 strlen( hc->hostname ) + 2 * VHOST_DIRLEVELS );
1413 if ( strncmp( hc->hostname, "www.", 4 ) == 0 )
1414 cp1 = &hc->hostname[4];
1416 cp1 = hc->hostname;
1417 for ( cp2 = hc->hostdir, i = 0; i < VHOST_DIRLEVELS; ++i )
1435 (void) strcpy( cp2, hc->hostname );
1437 httpd_realloc_str( &hc->hostdir, &hc->maxhostdir, strlen( hc->hostname ) );
1438 (void) strcpy( hc->hostdir, hc->hostname );
1442 len = strlen( hc->expnfilename );
1444 (void) strcpy( tempfilename, hc->expnfilename );
1446 &hc->expnfilename, &hc->maxexpnfilename,
1447 strlen( hc->hostdir ) + 1 + len );
1448 (void) strcpy( hc->expnfilename, hc->hostdir );
1449 (void) strcat( hc->expnfilename, "/" );
1450 (void) strcat( hc->expnfilename, tempfilename );
1689 httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc )
1694 if ( ! hc->initialized )
1696 hc->read_size = 0;
1697 httpd_realloc_str( &hc->read_buf, &hc->read_size, 500 );
1698 hc->maxdecodedurl =
1699 hc->maxorigfilename = hc->maxexpnfilename = hc->maxencodings =
1700 hc->maxpathinfo = hc->maxquery = hc->maxaccept =
1701 hc->maxaccepte = hc->maxreqhost = hc->maxhostdir =
1702 hc->maxremoteuser = hc->maxresponse = 0;
1704 hc->maxaltdir = 0;
1706 httpd_realloc_str( &hc->decodedurl, &hc->maxdecodedurl, 1 );
1707 httpd_realloc_str( &hc->origfilename, &hc->maxorigfilename, 1 );
1708 httpd_realloc_str( &hc->expnfilename, &hc->maxexpnfilename, 0 );
1709 httpd_realloc_str( &hc->encodings, &hc->maxencodings, 0 );
1710 httpd_realloc_str( &hc->pathinfo, &hc->maxpathinfo, 0 );
1711 httpd_realloc_str( &hc->query, &hc->maxquery, 0 );
1712 httpd_realloc_str( &hc->accept, &hc->maxaccept, 0 );
1713 httpd_realloc_str( &hc->accepte, &hc->maxaccepte, 0 );
1714 httpd_realloc_str( &hc->reqhost, &hc->maxreqhost, 0 );
1715 httpd_realloc_str( &hc->hostdir, &hc->maxhostdir, 0 );
1716 httpd_realloc_str( &hc->remoteuser, &hc->maxremoteuser, 0 );
1717 httpd_realloc_str( &hc->response, &hc->maxresponse, 0 );
1719 httpd_realloc_str( &hc->altdir, &hc->maxaltdir, 0 );
1721 hc->initialized = 1;
1726 hc->conn_fd = accept( listen_fd, &sa.sa, &sz );
1727 if ( hc->conn_fd < 0 )
1741 close( hc->conn_fd );
1742 hc->conn_fd = -1;
1745 (void) fcntl( hc->conn_fd, F_SETFD, 1 );
1746 hc->hs = hs;
1747 (void) memset( &hc->client_addr, 0, sizeof(hc->client_addr) );
1748 (void) memmove( &hc->client_addr, &sa, sockaddr_len( &sa ) );
1749 hc->read_idx = 0;
1750 hc->checked_idx = 0;
1751 hc->checked_state = CHST_FIRSTWORD;
1752 hc->method = METHOD_UNKNOWN;
1753 hc->status = 0;
1754 hc->bytes_to_send = 0;
1755 hc->bytes_sent = 0;
1756 hc->encodedurl = "";
1757 hc->decodedurl[0] = '\0';
1758 hc->protocol = "UNKNOWN";
1759 hc->origfilename[0] = '\0';
1760 hc->expnfilename[0] = '\0';
1761 hc->encodings[0] = '\0';
1762 hc->pathinfo[0] = '\0';
1763 hc->query[0] = '\0';
1764 hc->referrer = "";
1765 hc->useragent = "";
1766 hc->accept[0] = '\0';
1767 hc->accepte[0] = '\0';
1768 hc->acceptl = "";
1769 hc->cookie = "";
1770 hc->contenttype = "";
1771 hc->reqhost[0] = '\0';
1772 hc->hdrhost = "";
1773 hc->hostdir[0] = '\0';
1774 hc->authorization = "";
1775 hc->remoteuser[0] = '\0';
1776 hc->response[0] = '\0';
1778 hc->altdir[0] = '\0';
1780 hc->responselen = 0;
1781 hc->if_modified_since = (time_t) -1;
1782 hc->range_if = (time_t) -1;
1783 hc->contentlength = -1;
1784 hc->type = "";
1785 hc->hostname = (char*) 0;
1786 hc->mime_flag = 1;
1787 hc->one_one = 0;
1788 hc->got_range = 0;
1789 hc->tildemapped = 0;
1790 hc->first_byte_index = 0;
1791 hc->last_byte_index = -1;
1792 hc->keep_alive = 0;
1793 hc->should_linger = 0;
1794 hc->processed_directory_index = 0;
1795 hc->file_address = (char*) 0;
1800 /* Checks hc->read_buf to see whether a complete request has been read so far;
1804 ** hc->read_idx is how much has been read in; hc->checked_idx is how much we
1805 ** have checked so far; and hc->checked_state is the current state of the
1809 httpd_got_request( httpd_conn* hc )
1813 for ( ; hc->checked_idx < hc->read_idx; ++hc->checked_idx )
1815 c = hc->read_buf[hc->checked_idx];
1816 switch ( hc->checked_state )
1822 hc->checked_state = CHST_FIRSTWS;
1825 hc->checked_state = CHST_BOGUS;
1835 hc->checked_state = CHST_BOGUS;
1838 hc->checked_state = CHST_SECONDWORD;
1846 hc->checked_state = CHST_SECONDWS;
1859 hc->checked_state = CHST_BOGUS;
1862 hc->checked_state = CHST_THIRDWORD;
1870 hc->checked_state = CHST_THIRDWS;
1873 hc->checked_state = CHST_LF;
1876 hc->checked_state = CHST_CR;
1886 hc->checked_state = CHST_LF;
1889 hc->checked_state = CHST_CR;
1892 hc->checked_state = CHST_BOGUS;
1900 hc->checked_state = CHST_LF;
1903 hc->checked_state = CHST_CR;
1914 hc->checked_state = CHST_CR;
1917 hc->checked_state = CHST_LINE;
1925 hc->checked_state = CHST_CRLF;
1931 hc->checked_state = CHST_LINE;
1942 hc->checked_state = CHST_CRLFCR;
1945 hc->checked_state = CHST_LINE;
1956 hc->checked_state = CHST_LINE;
1969 httpd_parse_request( httpd_conn* hc )
1980 hc->checked_idx = 0; /* reset */
1981 method_str = bufgets( hc );
1985 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
1994 hc->mime_flag = 0;
2006 hc->one_one = 1;
2009 hc->protocol = protocol;
2014 if ( ! hc->one_one )
2016 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2023 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2029 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2032 httpd_realloc_str( &hc->reqhost, &hc->maxreqhost, strlen( reqhost ) );
2033 (void) strcpy( hc->reqhost, reqhost );
2039 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2044 hc->method = METHOD_GET;
2046 hc->method = METHOD_HEAD;
2048 hc->method = METHOD_POST;*/
2050 hc->method = METHOD_PUT;
2052 hc->method = METHOD_DELETE;
2054 hc->method = METHOD_TRACE;
2057 httpd_send_err( hc, 501, err501title, "", err501form, method_str );
2061 hc->encodedurl = url;
2063 &hc->decodedurl, &hc->maxdecodedurl, strlen( hc->encodedurl ) );
2064 strdecode( hc->decodedurl, hc->encodedurl );
2067 &hc->origfilename, &hc->maxorigfilename, strlen( hc->decodedurl ) );
2068 (void) strcpy( hc->origfilename, &hc->decodedurl[1] );
2070 if ( hc->origfilename[0] == '\0' )
2071 (void) strcpy( hc->origfilename, "." );
2074 cp = strchr( hc->encodedurl, '?' );
2078 httpd_realloc_str( &hc->query, &hc->maxquery, strlen( cp ) );
2079 (void) strcpy( hc->query, cp );
2081 cp = strchr( hc->origfilename, '?' );
2086 de_dotdot( hc->origfilename );
2087 if ( hc->origfilename[0] == '/' ||
2088 ( hc->origfilename[0] == '.' && hc->origfilename[1] == '.' &&
2089 ( hc->origfilename[2] == '\0' || hc->origfilename[2] == '/' ) ) )
2091 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2095 if ( hc->mime_flag )
2098 while ( ( buf = bufgets( hc ) ) != (char*) 0 )
2106 hc->referrer = cp;
2112 hc->referrer = cp;
2118 hc->useragent = cp;
2124 hc->hdrhost = cp;
2125 cp = strchr( hc->hdrhost, ':' );
2128 if ( strchr( hc->hdrhost, '/' ) != (char*) 0 || hc->hdrhost[0] == '.' )
2130 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2138 if ( hc->accept[0] != '\0' )
2140 if ( strlen( hc->accept ) > 5000 )
2144 // httpd_ntoa( &hc->client_addr ) );
2148 &hc->accept, &hc->maxaccept,
2149 strlen( hc->accept ) + 2 + strlen( cp ) );
2150 (void) strcat( hc->accept, ", " );
2154 &hc->accept, &hc->maxaccept, strlen( cp ) );
2155 (void) strcat( hc->accept, cp );
2161 if ( hc->accepte[0] != '\0' )
2163 if ( strlen( hc->accepte ) > 5000 )
2167 // httpd_ntoa( &hc->client_addr ) );
2171 &hc->accepte, &hc->maxaccepte,
2172 strlen( hc->accepte ) + 2 + strlen( cp ) );
2173 (void) strcat( hc->accepte, ", " );
2177 &hc->accepte, &hc->maxaccepte, strlen( cp ) );
2178 (void) strcpy( hc->accepte, cp );
2184 hc->acceptl = cp;
2189 hc->if_modified_since = tdate_parse( cp );
2190 if ( hc->if_modified_since == (time_t) -1 )
2197 hc->cookie = cp;
2212 hc->got_range = 1;
2213 hc->first_byte_index = atoll( cp + 1 );
2214 if ( hc->first_byte_index < 0 )
2215 hc->first_byte_index = 0;
2218 hc->last_byte_index = atoll( cp_dash + 1 );
2219 if ( hc->last_byte_index < 0 )
2220 hc->last_byte_index = -1;
2230 hc->range_if = tdate_parse( cp );
2231 if ( hc->range_if == (time_t) -1 )
2238 hc->contenttype = cp;
2243 hc->contentlength = atol( cp );
2249 hc->authorization = cp;
2256 hc->keep_alive = 1;
2295 if ( hc->one_one )
2298 if ( hc->reqhost[0] == '\0' && hc->hdrhost[0] == '\0' )
2300 httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
2310 if ( hc->keep_alive )
2311 hc->should_linger = 1;
2320 &hc->expnfilename, &hc->maxexpnfilename, strlen( hc->origfilename ) );
2321 (void) strcpy( hc->expnfilename, hc->origfilename );
2324 if ( hc->expnfilename[0] == '~' )
2327 if ( ! tilde_map_1( hc ) )
2329 httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl );
2334 if ( ! tilde_map_2( hc ) )
2336 httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl );
2343 if ( hc->hs->vhost )
2344 if ( ! vhost_map( hc ) )
2346 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
2353 cp = expand_symlinks( hc->expnfilename, &pi, hc->hs->no_symlink_check, hc->tildemapped );
2356 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
2359 httpd_realloc_str( &hc->expnfilename, &hc->maxexpnfilename, strlen( cp ) );
2360 (void) strcpy( hc->expnfilename, cp );
2361 httpd_realloc_str( &hc->pathinfo, &hc->maxpathinfo, strlen( pi ) );
2362 (void) strcpy( hc->pathinfo, pi );
2365 if ( hc->pathinfo[0] != '\0' )
2368 i = strlen( hc->origfilename ) - strlen( hc->pathinfo );
2369 if ( i > 0 && strcmp( &hc->origfilename[i], hc->pathinfo ) == 0 )
2370 hc->origfilename[i - 1] = '\0';
2376 if ( hc->expnfilename[0] == '/' )
2379 hc->expnfilename, hc->hs->cwd, strlen( hc->hs->cwd ) ) == 0 )
2383 hc->expnfilename, &hc->expnfilename[strlen( hc->hs->cwd )] );
2386 else if ( hc->altdir[0] != '\0' &&
2388 hc->expnfilename, hc->altdir,
2389 strlen( hc->altdir ) ) == 0 &&
2390 ( hc->expnfilename[strlen( hc->altdir )] == '\0' ||
2391 hc->expnfilename[strlen( hc->altdir )] == '/' ) ) )
2398 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
2400 hc, 403, err403title, "",
2402 hc->encodedurl );
2412 bufgets( httpd_conn* hc )
2417 for ( i = hc->checked_idx; hc->checked_idx < hc->read_idx; ++hc->checked_idx )
2419 c = hc->read_buf[hc->checked_idx];
2422 hc->read_buf[hc->checked_idx] = '\0';
2423 ++hc->checked_idx;
2424 if ( c == '\015' && hc->checked_idx < hc->read_idx &&
2425 hc->read_buf[hc->checked_idx] == '\012' )
2427 hc->read_buf[hc->checked_idx] = '\0';
2428 ++hc->checked_idx;
2430 return &(hc->read_buf[i]);
2485 httpd_close_conn( httpd_conn* hc, struct timeval* nowP )
2487 make_log_entry( hc, nowP );
2489 /* if ( hc->file_address != (char*) 0 )
2491 mmc_unmap( hc->file_address, &(hc->sb), nowP );
2492 hc->file_address = (char*) 0;
2494 if ( hc->conn_fd >= 0 )
2496 (void) close( hc->conn_fd );
2497 hc->conn_fd = -1;
2502 httpd_destroy_conn( httpd_conn* hc )
2504 if ( hc->initialized )
2506 free( (void*) hc->read_buf );
2507 free( (void*) hc->decodedurl );
2508 free( (void*) hc->origfilename );
2509 free( (void*) hc->expnfilename );
2510 free( (void*) hc->encodings );
2511 free( (void*) hc->pathinfo );
2512 free( (void*) hc->query );
2513 free( (void*) hc->accept );
2514 free( (void*) hc->accepte );
2515 free( (void*) hc->reqhost );
2516 free( (void*) hc->hostdir );
2517 free( (void*) hc->remoteuser );
2518 free( (void*) hc->response );
2520 free( (void*) hc->altdir );
2522 hc->initialized = 0;
2583 figure_mime( httpd_conn* hc )
2596 for ( prev_dot = &hc->expnfilename[strlen(hc->expnfilename)]; ; prev_dot = dot )
2598 for ( dot = prev_dot - 1; dot >= hc->expnfilename && *dot != '.'; --dot )
2600 if ( dot < hc->expnfilename )
2605 hc->type = default_type;
2649 hc->type = typ_tab[mid].val;
2653 hc->type = default_type;
2658 hc->encodings[0] = '\0';
2663 &hc->encodings, &hc->maxencodings,
2665 if ( hc->encodings[0] != '\0' )
2667 (void) strcpy( &hc->encodings[encodings_len], "," );
2670 (void) strcpy( &hc->encodings[encodings_len], enc_tab[me_indexes[i]].val );
2721 ls( httpd_conn* hc )
2749 dirp = opendir( hc->expnfilename );
2753 sprintf(logString, "Error 404 File not found: %s\n", hc->decodedurl+1);
2754 poorman_log(logString, true, &hc->client_addr, RED);
2755 // syslog( LOG_ERR, "opendir %.80s - %m", hc->expnfilename );
2756 httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl );
2760 if ( hc->method == METHOD_HEAD )
2764 hc, 200, ok200title, "", "", "text/html; charset=%s", (off_t) -1,
2765 hc->sb.st_mtime );
2766 httpd_write_response( hc );
2768 else if ( hc->method == METHOD_GET )
2776 hc->hs->cwd,
2777 hc->expnfilename
2785 strcat(logString, hc->hs->index_name);
2792 poorman_log(logString, true, &hc->client_addr, BLACK);
2795 hc, 200, ok200title, "", "", "text/html; charset=%s",
2796 (off_t) -1, hc->sb.st_mtime );
2797 httpd_write_response( hc );
2809 fp = fdopen( hc->conn_fd, "w" );
2814 hc, 500, err500title, "", err500form, hc->encodedurl );
2815 httpd_write_response( hc );
2837 hc->encodedurl, hc->encodedurl );
2881 strlen( hc->expnfilename ) + 1 + strlen( nameptrs[i] ) );
2884 strlen( hc->origfilename ) + 1 + strlen( nameptrs[i] ) );
2885 if ( hc->expnfilename[0] == '\0' ||
2886 strcmp( hc->expnfilename, "." ) == 0 )
2894 "%s/%s", hc->expnfilename, nameptrs[i] );
2895 if ( strcmp( hc->origfilename, "." ) == 0 )
2900 "%s%s", hc->origfilename, nameptrs[i] );
2995 // syslog( LOG_DEBUG, "spawned indexing process %d for directory '%.200s'", r, hc->expnfilename );
3005 hc->status = 200;
3006 hc->bytes_sent = CGI_BYTECOUNT;
3007 hc->should_linger = 0;
3019 hc, 501, err501title, "", err501form, httpd_method_str( hc->method ) );
3022 hc->processed_directory_index = 1;
3073 make_envp( httpd_conn* hc )
3086 if ( hc->hs->vhost && hc->hostname != (char*) 0 && hc->hostname[0] != '\0' )
3087 cp = hc->hostname;
3088 else if ( hc->hdrhost != (char*) 0 && hc->hdrhost[0] != '\0' )
3089 cp = hc->hdrhost;
3090 else if ( hc->reqhost != (char*) 0 && hc->reqhost[0] != '\0' )
3091 cp = hc->reqhost;
3093 cp = hc->hs->server_hostname;
3097 envp[envn++] = build_env("SERVER_PROTOCOL=%s", hc->protocol);
3098 (void) my_snprintf( buf, sizeof(buf), "%d", (int) hc->hs->port );
3101 "REQUEST_METHOD=%s", httpd_method_str( hc->method ) );
3102 if ( hc->pathinfo[0] != '\0' )
3106 envp[envn++] = build_env( "PATH_INFO=/%s", hc->pathinfo );
3107 l = strlen( hc->hs->cwd ) + strlen( hc->pathinfo ) + 1;
3111 (void) my_snprintf( cp2, l, "%s%s", hc->hs->cwd, hc->pathinfo );
3116 "SCRIPT_NAME=/%s", strcmp( hc->origfilename, "." ) == 0 ?
3117 "" : hc->origfilename );
3118 if ( hc->query[0] != '\0')
3119 envp[envn++] = build_env( "QUERY_STRING=%s", hc->query );
3121 "REMOTE_ADDR=%s", httpd_ntoa( &hc->client_addr ) );
3122 if ( hc->referrer[0] != '\0' )
3124 envp[envn++] = build_env( "HTTP_REFERER=%s", hc->referrer );
3125 envp[envn++] = build_env( "HTTP_REFERRER=%s", hc->referrer );
3127 if ( hc->useragent[0] != '\0' )
3128 envp[envn++] = build_env( "HTTP_USER_AGENT=%s", hc->useragent );
3129 if ( hc->accept[0] != '\0' )
3130 envp[envn++] = build_env( "HTTP_ACCEPT=%s", hc->accept );
3131 if ( hc->accepte[0] != '\0' )
3132 envp[envn++] = build_env( "HTTP_ACCEPT_ENCODING=%s", hc->accepte );
3133 if ( hc->acceptl[0] != '\0' )
3134 envp[envn++] = build_env( "HTTP_ACCEPT_LANGUAGE=%s", hc->acceptl );
3135 if ( hc->cookie[0] != '\0' )
3136 envp[envn++] = build_env( "HTTP_COOKIE=%s", hc->cookie );
3137 if ( hc->contenttype[0] != '\0' )
3138 envp[envn++] = build_env( "CONTENT_TYPE=%s", hc->contenttype );
3139 if ( hc->hdrhost[0] != '\0' )
3140 envp[envn++] = build_env( "HTTP_HOST=%s", hc->hdrhost );
3141 if ( hc->contentlength != -1 )
3144 buf, sizeof(buf), "%lu", (unsigned long) hc->contentlength );
3147 if ( hc->remoteuser[0] != '\0' )
3148 envp[envn++] = build_env( "REMOTE_USER=%s", hc->remoteuser );
3149 if ( hc->authorization[0] != '\0' )
3154 envp[envn++] = build_env( "CGI_PATTERN=%s", hc->hs->cgi_pattern );
3163 ** scribble on hc->query.
3166 make_argp( httpd_conn* hc )
3177 argp = NEW( char*, strlen( hc->query ) + 2 );
3181 argp[0] = strrchr( hc->expnfilename, '/' );
3185 argp[0] = hc->expnfilename;
3193 if ( strchr( hc->query, '=' ) == (char*) 0 )
3195 for ( cp1 = cp2 = hc->query; *cp2 != '\0'; ++cp2 )
3224 cgi_interpose_input( httpd_conn* hc, int wfd )
3230 c = hc->read_idx - hc->checked_idx;
3233 if ( httpd_write_fully( wfd, &(hc->read_buf[hc->checked_idx]), c ) != c )
3236 while ( c < hc->contentlength )
3238 r = read( hc->conn_fd, buf, MIN( sizeof(buf), hc->contentlength - c ) );
3250 post_post_garbage_hack( hc );
3263 post_post_garbage_hack( httpd_conn* hc )
3271 httpd_set_ndelay( hc->conn_fd );
3273 (void) read( hc->conn_fd, buf, sizeof(buf) );
3285 cgi_interpose_output( httpd_conn* hc, int rfd )
3299 httpd_clear_ndelay( hc->conn_fd );
3375 (void) httpd_write_fully( hc->conn_fd, buf, strlen( buf ) );
3378 (void) httpd_write_fully( hc->conn_fd, headers, headers_len );
3391 if ( httpd_write_fully( hc->conn_fd, buf, r ) != r )
3394 shutdown( hc->conn_fd, SHUT_WR );
3400 cgi_child( httpd_conn* hc )
3415 (void) fcntl( hc->conn_fd, F_SETFD, 0 );
3432 if ( hc->conn_fd == STDIN_FILENO || hc->conn_fd == STDOUT_FILENO || hc->conn_fd == STDERR_FILENO )
3434 int newfd = dup2( hc->conn_fd, STDERR_FILENO + 1 );
3436 hc->conn_fd = newfd;
3443 // envp = make_envp( hc );
3446 argp = make_argp( hc );
3452 if ( hc->method == METHOD_POST && hc->read_idx > hc->checked_idx )
3459 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3460 httpd_write_response( hc );
3467 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3468 httpd_write_response( hc );
3476 cgi_interpose_input( hc, p[1] );
3490 if ( hc->conn_fd != STDIN_FILENO )
3491 (void) dup2( hc->conn_fd, STDIN_FILENO );
3497 if ( strncmp( argp[0], "nph-", 4 ) != 0 && hc->mime_flag )
3504 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3505 httpd_write_response( hc );
3512 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3513 httpd_write_response( hc );
3521 cgi_interpose_output( hc, p[0] );
3536 if ( hc->conn_fd != STDOUT_FILENO )
3537 (void) dup2( hc->conn_fd, STDOUT_FILENO );
3538 if ( hc->conn_fd != STDERR_FILENO )
3539 (void) dup2( hc->conn_fd, STDERR_FILENO );
3542 /* At this point we would like to set close-on-exec again for hc->conn_fd
3553 /* (void) fcntl( hc->conn_fd, F_SETFD, 1 ); */
3564 directory = strdup( hc->expnfilename );
3566 binary = hc->expnfilename; /* ignore errors */
3571 binary = hc->expnfilename;
3590 // syslog( LOG_ERR, "execve %.80s - %m", hc->expnfilename );
3591 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3592 httpd_write_response( hc );
3598 cgi( httpd_conn* hc )
3603 if ( hc->hs->cgi_limit != 0 && hc->hs->cgi_count >= hc->hs->cgi_limit )
3606 hc, 503, httpd_err503title, "", httpd_err503form,
3607 hc->encodedurl );
3610 ++hc->hs->cgi_count;
3611 httpd_clear_ndelay( hc->conn_fd );
3617 hc, 500, err500title, "", err500form, hc->encodedurl );
3624 httpd_unlisten( hc->hs );
3625 cgi_child( hc );
3629 // syslog( LOG_DEBUG, "spawned CGI process %d for file '%.200s'", r, hc->expnfilename );
3639 hc->status = 200;
3640 hc->bytes_sent = CGI_BYTECOUNT;
3641 hc->should_linger = 0;
3648 really_start_request( httpd_conn* hc, struct timeval* nowP )
3662 expnlen = strlen( hc->expnfilename );
3665 if ( stat( hc->expnfilename, &hc->sb ) < 0 )
3667 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3676 if ( ! ( hc->sb.st_mode & ( S_IROTH /*| S_IXOTH*/ ) ) )
3681 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3683 hc, 403, err403title, "",
3685 hc->encodedurl );
3690 if ( S_ISDIR(hc->sb.st_mode) )
3693 if ( hc->pathinfo[0] != '\0' )
3696 sprintf(logString, "Error 404 File not found: %s\n", hc->decodedurl+1);
3697 poorman_log(logString, true, &hc->client_addr, RED);
3698 httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl );
3706 if ( strcmp( hc->origfilename, "" ) != 0 &&
3707 strcmp( hc->origfilename, "." ) != 0 &&
3708 hc->origfilename[strlen( hc->origfilename ) - 1] != '/' )
3710 send_dirredirect( hc );
3718 expnlen + 1 + strlen( /*index_names[i]*/hc->hs->index_name ) );
3719 (void) strcpy( indexname, hc->expnfilename );
3725 (void) strcat( indexname, /*index_names[i]*/hc->hs->index_name );
3729 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3732 if ( stat( indexname, &hc->sb ) >= 0 )
3738 if(hc->hs->do_list_dir){
3740 if ( ! ( hc->sb.st_mode & S_IROTH ) )
3745 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3747 hc, 403, err403title, "",
3749 hc->encodedurl );
3754 if ( auth_check( hc, hc->expnfilename ) == -1 )
3758 if ( ! check_referrer( hc ) )
3761 return ls( hc );
3766 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3768 hc, 403, err403title, "",
3770 hc->encodedurl );
3779 cp = expand_symlinks( indexname, &pi, hc->hs->no_symlink_check, hc->tildemapped );
3782 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3786 httpd_realloc_str( &hc->expnfilename, &hc->maxexpnfilename, expnlen );
3787 (void) strcpy( hc->expnfilename, cp );
3790 if ( ! ( hc->sb.st_mode & ( S_IROTH | S_IXOTH ) ) )
3795 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3797 hc, 403, err403title, "",
3799 hc->encodedurl );
3807 (void) strcpy( dirname, hc->expnfilename );
3813 if ( auth_check( hc, dirname ) == -1 )
3819 if ( strcmp( hc->expnfilename, AUTH_FILE ) == 0 )
3824 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3826 hc, 403, err403title, "",
3828 hc->encodedurl );
3833 strcmp( &(hc->expnfilename[expnlen - sizeof(AUTH_FILE) + 1]), AUTH_FILE ) == 0 &&
3834 hc->expnfilename[expnlen - sizeof(AUTH_FILE)] == '/' )
3839 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3841 hc, 403, err403title, "",
3843 hc->encodedurl );
3849 if ( ! check_referrer( hc ) )
3853 /*if ( hc->hs->cgi_pattern != (char*) 0 &&
3854 ( hc->sb.st_mode & S_IXOTH ) &&
3855 match( hc->hs->cgi_pattern, hc->expnfilename ) )
3856 return cgi( hc );*/
3862 /*if ( hc->sb.st_mode & S_IXOTH )
3866 httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3868 hc, 403, err403title, "",
3870 hc->encodedurl );
3873 if ( hc->pathinfo[0] != '\0' )
3877 // httpd_ntoa( &hc->client_addr ), hc->encodedurl );
3879 hc, 403, err403title, "",
3881 hc->encodedurl );
3885 if ( hc->method != METHOD_GET && hc->method != METHOD_HEAD )
3888 hc, 501, err501title, "", err501form, httpd_method_str( hc->method ) );
3893 if ( hc->got_range &&
3894 ( hc->last_byte_index == -1 || hc->last_byte_index >= hc->sb.st_size ) )
3895 hc->last_byte_index = hc->sb.st_size - 1;
3897 figure_mime( hc );
3899 if ( hc->method == METHOD_HEAD )
3902 hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size,
3903 hc->sb.st_mtime );
3905 else if ( hc->if_modified_since != (time_t) -1 &&
3906 hc->if_modified_since >= hc->sb.st_mtime )
3909 hc, 304, err304title, hc->encodings, "", hc->type, (off_t) -1,
3910 hc->sb.st_mtime );
3914 /* hc->file_address = mmc_map( hc->expnfilename, &(hc->sb), nowP );
3915 if ( hc->file_address == (char*) 0 )
3917 httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
3921 hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size,
3922 hc->sb.st_mtime );
3930 httpd_start_request( httpd_conn* hc, struct timeval* nowP )
3935 r = really_start_request( hc, nowP );
3943 make_log_entry( httpd_conn* hc, struct timeval* nowP )
3949 if ( hc->hs->no_log )
3959 if ( hc->remoteuser[0] != '\0' )
3960 ru = hc->remoteuser;
3967 if ( hc->hs->vhost && ! hc->tildemapped )
3970 hc->hostname == (char*) 0 ? hc->hs->server_hostname : hc->hostname,
3971 hc->encodedurl );
3974 "%.200s", hc->encodedurl );
3976 if ( hc->bytes_sent >= 0 )
3978 bytes, sizeof(bytes), "%lld", (long long) hc->bytes_sent );
3983 if ( hc->hs->logfp != (FILE*) 0 )
4020 (void) fprintf( hc->hs->logfp,
4022 httpd_ntoa( &hc->client_addr ), ru, date,
4023 httpd_method_str( hc->method ), url, hc->protocol,
4024 hc->status, bytes, hc->referrer, hc->useragent );
4026 (void) fflush( hc->hs->logfp );
4032 httpd_ntoa( &hc->client_addr ), ru,
4033 httpd_method_str( hc->method ), url, hc->protocol,
4034 hc->status, bytes, hc->referrer, hc->useragent )*/;
4040 check_referrer( httpd_conn* hc )
4046 if ( hc->hs->url_pattern == (char*) 0 )
4049 r = really_check_referrer( hc );
4053 if ( hc->hs->vhost && hc->hostname != (char*) 0 )
4054 cp = hc->hostname;
4056 cp = hc->hs->server_hostname;
4061 // httpd_ntoa( &hc->client_addr ), cp, hc->encodedurl, hc->referrer );
4063 hc, 403, err403title, "",
4065 hc->encodedurl );
4073 really_check_referrer( httpd_conn* hc )
4083 hs = hc->hs;
4086 if ( hc->referrer == (char*) 0 || hc->referrer[0] == '\0' ||
4087 ( cp1 = strstr( hc->referrer, "//" ) ) == (char*) 0 )
4090 if ( hs->no_empty_referrers && match( hs->url_pattern, hc->origfilename ) )
4125 lp = hc->hostname;
4139 if ( ! match( lp, refhost ) && match( hs->url_pattern, hc->origfilename ) )