void read(HTTP_REQUEST& http_request) { read(http_request.header); read(http_request.body, http_request.header); } void read(HTTP_REQUEST_HEADER& header) { string line = read_line(); parse_request_link(line, header.method, header.uri, header.version); while (TRUE) { line = read_line(); if (line.empty()) break; parse_header_field(line, header); } } void read(BYTE[]& body, HTTP_REQUEST_HEADER& header) { string transfer_encoding = header.fields['Transfer-Encoding']; if (transfer_encoding != b.chunkedb.) body = read_bytes(header.fields['Content-Length']); else { while (TRUE) { string chunk_header = read_line(); DWORD chunk_size = atoi(chunk_header); if (chunk_size == 0) break; BYTE[] chunk_body = read_bytes(chunk_size); body += chunk_body; } } } string read_line() { while (TRUE) { int n = strpos(read_buffer, b. b., read_buffer.size()); if (n > 0) break; read_buffer += socket.read(); } return read_buffer.extract(n); } Byte[] read_bytes(int sz) { while (TRUE) { if (sz <= read_buffer.size()) break; read_buffer += socket.read(); } return read_buffer.extract(sz); } |
在这段代码中,执行流程与逻辑流程是一致的,然而,如果在那些被动接收事件的场合使用了异步套接字,就必须编写下面这样的代码了: