⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpd_main.c

📁 网页的客户端开发
💻 C
📖 第 1 页 / 共 2 页
字号:
		httpd_realloc_str(&hc->reqhost, &hc->maxreqhost, 0);
		httpd_realloc_str(&hc->remoteuser, &hc->maxremoteuser, 0);
		httpd_realloc_str(&hc->response, &hc->maxresponse, 0);
		hc->initialized = 1;
	}

	// hc->hs = hs;
	// hc->client_addr = sin.sin_addr;
	hc->read_idx = 0;
	hc->checked_idx = 0;
	hc->checked_state = CHST_FIRSTWORD;
	hc->protocol = "HTTP/1.0";
	hc->mime_flag = 1;
	hc->should_linger = 0;
	hc->file_address = (char*) 0;
}


/*----------------------------------------------------------------------
* ROUTINE NAME - httpd_free_conn_table
*-----------------------------------------------------------------------
* DESCRIPTION: Free all allocated buffer in connection table
* INPUT      : HTTPD_CONN_T* hc
* OUTPUT     : None
*----------------------------------------------------------------------*/
static void httpd_free_conn_table(HTTPD_CONN_T* hc)
{
	// struct sockaddr_in sin;
	// int sz;

	if (! hc->initialized) return;

    if (hc->decodedurl)     httpd_free(hc->decodedurl);
	if (hc->origfilename)   httpd_free(hc->origfilename);
	if (hc->expnfilename)   httpd_free(hc->expnfilename); 
	if (hc->encodings)      httpd_free(hc->encodings);
	if (hc->pathinfo)       httpd_free(hc->pathinfo);
	if (hc->query)          httpd_free(hc->query);
	if (hc->accept)         httpd_free(hc->accept);
	if (hc->accepte)        httpd_free(hc->accepte);
	if (hc->reqhost)        httpd_free(hc->reqhost);
	if (hc->remoteuser)     httpd_free(hc->remoteuser);
	if (hc->response)       httpd_free(hc->response);
	
	if (hc->file_address)   httpd_free(hc->file_address);
    
    hc->initialized = 0;
}

/*----------------------------------------------------------------------
* ROUTINE NAME - httpd_recv
*-----------------------------------------------------------------------
* DESCRIPTION: Receive data for httpd
* INPUT      : int conn_fd
*              char *buf 
*              int len
*              int flags
* OUTPUT     : None
*----------------------------------------------------------------------*/
int httpd_recv (int conn_fd, char *buf, int len, int flags)
{
	int      rc;
	fd_set   fds;
	struct   timeval tv;

	tv.tv_sec = 3;     
	tv.tv_usec = 0;    

	FD_ZERO(&fds);
	FD_SET(conn_fd, &fds);

    rc = select(conn_fd+1, &fds, 0, 0, &tv);  
	if (rc <=0 )            
		return -1;
			
	if (FD_ISSET(conn_fd, &fds))
	{                     
		return recv(conn_fd, buf, len, flags);
	}
	return -1;
}

/*----------------------------------------------------------------------
* ROUTINE NAME - httpd_realloc_str
*-----------------------------------------------------------------------
* DESCRIPTION: Reallocate memory 
* INPUT      : char** strP
*              int* maxsizeP
*              int size
* OUTPUT     : None
*----------------------------------------------------------------------*/
void httpd_realloc_str(char** strP, int* maxsizeP, int size)
{
	if (*maxsizeP == 0)
	{
		*maxsizeP = MAX(200, size);	/* arbitrary */
		*strP = HTTPD_NEW(char, *maxsizeP + 1);
	}
	else if (size > *maxsizeP)
	{
		*maxsizeP = MAX(*maxsizeP * 2, size * 5 / 4);
		*strP = HTTPD_RENEW(*strP, char, *maxsizeP + 1);
	}
	else
		return;

}


/*----------------------------------------------------------------------
* ROUTINE NAME - httpd_handle_read
*-----------------------------------------------------------------------
* DESCRIPTION: Handle http read process 
* INPUT      : HTTPD_CONN_T* hc
* OUTPUT     : None
*----------------------------------------------------------------------*/
static void httpd_handle_read (HTTPD_CONN_T* hc)
{
	int sz;
	// ClientData client_data;

	/* Is there room in our buffer to read more bytes? */
	if (hc->read_idx >= sizeof(hc->read_buf))
	{
		httpd_send_err (hc, 400, httpd_err400title, httpd_err400form, "");
		return;
	}

	/* Read some more bytes. */
	sz = httpd_recv (hc->conn_fd, &(hc->read_buf [hc->read_idx]), sizeof (hc->read_buf) - hc->read_idx, 0);
	if (sz <= 0)
	{
		httpd_send_err (hc, 400, httpd_err400title, httpd_err400form, "");
		return;
	}
	hc->read_idx += sz;

	/* Do we have a complete request yet? */
	switch (httpd_got_request(hc))
	{
		case GR_NO_REQUEST:
		    return;

		case GR_BAD_REQUEST:
			httpd_send_err(hc, 400, httpd_err400title, httpd_err400form, "");
			// clear_connection(c, tvP);
			return;
	}

	/* Yes.  Try parsing and resolving it. */
	if (httpd_parse_request (hc) < 0)
	{
		// clear_connection(c, tvP);
		return;
	}

	/* Start the connection going. */
	if (httpd_start_request(hc) < 0)
	{
		/* Something went wrong.  Close down the connection. */
		// clear_connection(c, tvP);
		return;
	}

}


/*----------------------------------------------------------------------
* ROUTINE NAME - httpd_handle_send
*-----------------------------------------------------------------------
* DESCRIPTION: Handle http send process 
* INPUT      : HTTPD_CONN_T* hc
* OUTPUT     : None
*----------------------------------------------------------------------*/
static void httpd_handle_send(HTTPD_CONN_T* hc)
{
    int sz, len, total;
    off_t bytes_sent=0;
    off_t bytes_to_send=0;
    char *cp;


    // move from handle_read() 
	if (hc->got_range)
	{
		bytes_sent = hc->init_byte_loc;
		bytes_to_send = hc->end_byte_loc + 1;
	}
	else
		bytes_to_send = hc->bytes;
	/* Check if it's already handled. */
	if (hc->file_address == (char*) 0)
	{
		/* No file address means someone else is handling it. */
		bytes_sent = hc->bytes;
		// return;
	}
    // end: move from handle_read() 
    

    /* Do we need to write the headers first? */
    if ( hc->responselen == 0 )
	{
	    /* No, just write the file. */
	    sz = write(hc->conn_fd, &hc->file_address[bytes_sent],
	               (bytes_to_send - bytes_sent) );
        if ( sz == 0 || sz < 0 )
	    {
	        // dprintf("<httpd_handle_send 1>Failed to send! Errno=%d\n",errno);
	        return;
	    }
	}
    else
	{
	    sz = write(hc->conn_fd, hc->response,hc->responselen);
        if ( sz == 0 || sz < 0 )
	    {
	        // dprintf("<httpd_handle_send 2>Failed to send! Errno=%d\n",errno);
	        return;
	    }
	    len=total=bytes_to_send - bytes_sent;
	    if (len)
	    {
	        cp=&hc->file_address[bytes_sent];
	        sz = write(hc->conn_fd,cp,len);
	        // dprintf("<httpd_handle_send 3>len=%d, sz=%d\n",len,sz);
            if ( sz == 0 || sz < 0 )
	        {
	            // dprintf("<httpd_handle_send 3>Failed to send! Errno=%d\n",errno);
	            return;
	        }
	    }
	}
}

void httpd_free(void *x) 
{
    if (x) 
    {
        httpd_free_cnt++; 
        free(x); 
    }
}

void *httpd_malloc(unsigned long x)
{
    void *rc;
    rc=malloc(x);
    if (rc) httpd_malloc_cnt++; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -