文章作者 100test 发表时间 2007:10:24 14:07:39
来源 100Test.Com百考试题网
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.\nb., 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). } |
read() { read_buffer = socket.read(). if (state == read_request_line) { if (!read_line(line)) return. parse_request_link(line, method, uri, version). state = read_header_field. } while (state == read_request_line) { if (!read_line(line)) return. if (line.empty()) { transfer_encoding = header.fields[ Transfer-Encoding ]. if (transfer_encoding != b.chunkedb.) { content_length = header.fields[ Content-Length ]. state = read_body. } else state = read_chunk_header. } else parse_header_field(line, header, value). } if (state == read_body) { request_body = read_buffer. read_buffer.clear(). if (request_body.size() >= content_length) state = read_finished. return. } if (state == read_chunk_header) { if (!read_line(line)) return. chunk_size = atoi(line). if (chunk_size == 0) { state = read_finished. return. } state = read_body. } if (state == read_chunk_body) { request_body.append(read_buffer, chunk_size). if (chunk_size == 0) state = read_chunk_header. return. } } |
执行流程完全不同了,但逻辑流程却仍保持不变,因为只能一块一块地接收数据,还必须保存状态值及其他变量,以便在事件发生时进行相应的处理。以上只是一些示范性代码,并不能真正工作,在实际中要编写像这样的函数会更加复杂,也更加容易出错。
相关文章
五个不太为人知的ESC键妙用法
WinXP下的8步快速设置无线网络
巧妙解决WindowsXP网络访问慢的难题
windows强化安全综合设置
在C 程序中添加逻辑流程控制
转:从VC 到GCC移植:谈两者语法差异
C_C 中动态链接库的创建和调用
谈函数指针(全局_类成员函数)和函数对象
A .NET效率陷阱之——Attributes
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛