实现了一个比Nginx速度更快的HTTP服务器(4)

void read_request ( struct process* process )
{
int sock = process->sock, s;
char* buf=process->buf;
char read_complete = 0;

ssize_t count;

while ( 1 )
{
count = read ( sock, buf + process->read_pos, BUF_SIZE - process->read_pos );
if ( count == -1 )
{
if ( errno != EAGAIN )
{
handle_error ( process, "read request" );
return;
}
else
{
//errno == EAGAIN表示读取完毕
break;
}
}
else if ( count == 0 )
{
// 被客户端关闭连接
cleanup ( process );
return;
}
else if ( count > 0 )
{
process->read_pos += count;
}
}

int header_length = process->read_pos;
// determine whether the request is complete
if ( header_length > BUF_SIZE - 1 )
{
process->response_code = 400;
process->status = STATUS_SEND_RESPONSE_HEADER;
strcpy ( process->buf, header_400 );
send_response_header ( process );
handle_error ( processes, "bad request" );
return;
}
buf[header_length]=0;
read_complete = ( strstr ( buf, "\n\n" ) != 0 ) || ( strstr ( buf, "\r\n\r\n" ) != 0 );

if ( read_complete )
{
// ...

//解析之后,打开文件,把文件描述符存入process,然后进入发送header状态
process->status = STATUS_SEND_RESPONSE_HEADER;
//修改此sock的监听状态,改为监视写状态
event.data.fd = process->sock;
event.events = EPOLLOUT | EPOLLET;
s = epoll_ctl ( efd, EPOLL_CTL_MOD, process->sock, &event );
if ( s == -1 )
{
perror ( "epoll_ctl" );
abort ();
}
//发送header
send_response_header ( process );
}
}

linux

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/wyyzwp.html