示例#1
0
文件: http.c 项目: whtc123/mysource
void httpsession_read(struct event *ev,int what, void *d)
{
	size_t len=0;
	size_t conten_len=0;
	char *line=NULL;
	char *ptr=NULL;
	//ptr=buff_tail_ptr()
	HttpSession *session = (HttpSession *)d;
	len=buff_read(session->fd, session->request.buff, buff_tail_left(session->request.buff));
	if(len<=0 )
		goto fail;
	while( (line=buff_get_line( session->request.buff,EOL_CRLF)) !=NULL )
	{
		if(session->request.prase_status==HPARSE_HEAD )
		{
			if( strlen(line)==0 )
			{
				session->request.prase_status=HPARSE_CONTENT;
				break;
			}
			if(http_parse_head(&session->request, line) == -1)
			{
				TraceErr("http_parse_head fail\n");
				goto fail;
			}
			continue;
		}

		if(session->request.prase_status==HPARSE_FIRST_LINE)
		{
			if(http_parse_first_line(&session->request,line)==-1)
				goto fail;
			session->request.prase_status=HPARSE_HEAD;
			continue;
		}

	}

	if(session->request.prase_status== HPARSE_CONTENT )
	{
		if(buff_len(session->request.buff) >= session->request.content_len)
		{
			session->request.prase_status = HPARSE_DONE;
			if(httpserver_route_request(session)!=200)goto fail;
			//if(session->server->request_callback(session,session->server->arg) != 0 )
			//	goto fail;
			//event_fd_setflags(session->ev,EPOLLIN|EPOLLOUT);
			//event_fd_setcallback(session->ev,httpsession_write);
			event_fd_setcallback(session->ev,httpsession_file_write);
			event_fd_setflags(session->ev,EPOLLOUT);
		}
	}
	return;	

fail:
	TraceErr("httpsession_read fail\n");
	httpserver_session_free(session);
	return;
}
示例#2
0
文件: httplib.c 项目: jzyuchen/Study
static void http_read_cb(evutil_socket_t fd, short events, void *arg)
{
	int ret = 0;
	struct http_connection *hc = arg;

	if (events & EV_TIMEOUT) {
		printf("timeout on fd %d, hc %p\n", fd, hc);
		goto failed;
	}

	if (events & EV_READ) {
		ret = evbuffer_read(hc->evbin, fd, 4096);
		if (ret == -1 || ret == 0) {
			goto failed;
		}

		if (hc->connection_status == HTTP_CONNECTION_STATUS_READ_HEADER) {
			if (evbuffer_find(hc->evbin, (const unsigned char *)"\r\n\r\n", 4) == NULL &&
				evbuffer_get_length(hc->evbin) < 4096)
			{
				// wait to read more data
				return;
			} else {
				//dbprintf("evbuffer_get_length: %d\n", evbuffer_get_length(hc->evbin));
			}

			ret = http_parse_first_line(hc, hc->evbin);
			if (ret) {
				goto failed;
			}

			ret = http_parse_headers(hc, hc->evbin);
			if (ret) {
				goto failed;
			}

			/* set no-timeout read */
			event_del(hc->ev_read);
			event_add(hc->ev_read, NULL);
			hc->connection_status = HTTP_CONNECTION_STATUS_READ_BODY;
		} else if (hc->connection_status == HTTP_CONNECTION_STATUS_READ_BODY) {

		}

		/* dispatch to handler */
		ret = http_dispatch_uri_handler_cb(hc);
		if (ret) {
			goto failed;
		}
	}

	return;
failed:
	http_connection_free(hc);
}