bool ofxAMFHTTPRequest::parseHTTPRequest(IOBuffer& buffer, IOBuffer& amfResultBuffer) { // step 1. parse http headers if(num_bytes_in_header == 0) { num_bytes_in_header = buffer.consumeUntil("\r\n\r\n", raw_headers); } // step 2. parse headers when all received. if(num_bytes_in_header != 0) { if(parseHTTPHeaders()) { num_bytes_in_body = headers["content-length"].getAsUInt32(); } } else { return false; } // step 3. parse body when received content-length. if(num_bytes_in_body != 0) { uint32_t bytes_stored = buffer.getNumBytesStored(); uint32_t bytes_total = num_bytes_in_header + num_bytes_in_body; uint32_t bytes_left = bytes_total - bytes_stored; if(bytes_left == 0) { amfResultBuffer.storeBuffer(buffer, num_bytes_in_body); //cout << headers.toString() << endl; return true; } } return false; }
// prepends the headers to the buffer for a http resonse. IOBuffer ofxAMFHTTPResponse::createHTTPResponse(IOBuffer& buffer) { IOBuffer http_buffer; http_buffer.setup(buffer.getNumBytesStored()); http_buffer.storeString("HTTP/1.1 200 OK\r\n"); stringstream ss; ss << "Content-Length: " << buffer.getNumBytesStored() << "\r\n"; http_buffer.storeString(ss.str()); http_buffer.storeString("Connection: close\r\n"); http_buffer.storeString("Content-type: application/x-amf\r\n"); http_buffer.storeString("\r\n"); http_buffer.storeBuffer(buffer); return http_buffer; }
// copy data from another buffer. void IOBuffer::storeBuffer(IOBuffer& other) { storeBuffer(other, other.getNumBytesStored()); }