From cf8c7cc3e4b2d4525629e46bc1d38dfd0cbe33b6 Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Tue, 24 Mar 2009 10:15:02 +0000 Subject: [PATCH] Change the debugger agent protocol to be RFC-822 compilant. All headers are now terminated with CRLF and not jusr LF. The beginning of the body is indicated by an empty header (just CRLF). Review URL: http://codereview.chromium.org/42553 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1588 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/debug-agent.cc | 137 ++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/src/debug-agent.cc b/src/debug-agent.cc index abf684de83..e865e0e291 100644 --- a/src/debug-agent.cc +++ b/src/debug-agent.cc @@ -96,7 +96,7 @@ void DebuggerAgent::CreateSession(Socket* client) { // If another session is already established terminate this one. if (session_ != NULL) { - static const char* message = "Remote debugging session already active\n"; + static const char* message = "Remote debugging session already active\r\n"; client->Send(message, strlen(message)); delete client; @@ -202,73 +202,70 @@ SmartPointer DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { int received; // Read header. - const int kHeaderBufferSize = 80; - char header_buffer[kHeaderBufferSize]; - int header_buffer_position = 0; - char c = '\0'; // One character receive buffer. - char last_c = '\0'; // Previous character. int content_length = 0; - while (!(c == '\n' && last_c == '\n')) { - last_c = c; - received = conn->Receive(&c, 1); - if (received <= 0) { - PrintF("Error %d\n", Socket::LastError()); - return SmartPointer(); - } + while (true) { + const int kHeaderBufferSize = 80; + char header_buffer[kHeaderBufferSize]; + int header_buffer_position = 0; + char c = '\0'; // One character receive buffer. + char prev_c = '\0'; // Previous character. - // Check for end of header line. - if (c == '\n') { - // Empty header line. - if (header_buffer_position == 0) { - continue; + // Read until CRLF. + while (!(c == '\n' && prev_c == '\r')) { + prev_c = c; + received = conn->Receive(&c, 1); + if (received <= 0) { + PrintF("Error %d\n", Socket::LastError()); + return SmartPointer(); } - // Terminate header. - ASSERT(header_buffer_position < kHeaderBufferSize); + // Add character to header buffer. if (header_buffer_position < kHeaderBufferSize) { - header_buffer[header_buffer_position] = '\0'; - } - - // Split header. - char* key = header_buffer; - char* value = NULL; - for (int i = 0; i < header_buffer_position; i++) { - if (header_buffer[i] == ':') { - header_buffer[i] = '\0'; - value = header_buffer + i + 1; - while (*value == ' ') { - value++; - } - break; - } - } - - // Check that key is Content-Length. - if (strcmp(key, kContentLength) == 0) { - // Get the content length value if within a sensible range. - if (strlen(value) > 7) { - return SmartPointer(); - } - for (int i = 0; value[i] != '\0'; i++) { - // Bail out if illegal data. - if (value[i] < '0' || value[i] > '9') { - return SmartPointer(); - } - content_length = 10 * content_length + (value[i] - '0'); - } - } else { - // For now just print all other headers than Content-Length. - PrintF("%s: %s\n", key, value); - } - - // Start collecting new header. - header_buffer_position = 0; - } else { - // Add character to header buffer (reserve room for terminating '\0'). - if (header_buffer_position < kHeaderBufferSize - 1) { header_buffer[header_buffer_position++] = c; } } + + // Check for end of header (empty header line). + if (header_buffer_position == 2) { // Receive buffer contains CRLF. + break; + } + + // Terminate header. + ASSERT(header_buffer_position > 1); // At least CRLF is received. + ASSERT(header_buffer_position <= kHeaderBufferSize); + header_buffer[header_buffer_position - 2] = '\0'; + + // Split header. + char* key = header_buffer; + char* value = NULL; + for (int i = 0; header_buffer[i] != '\0'; i++) { + if (header_buffer[i] == ':') { + header_buffer[i] = '\0'; + value = header_buffer + i + 1; + while (*value == ' ') { + value++; + } + break; + } + } + + // Check that key is Content-Length. + if (strcmp(key, kContentLength) == 0) { + // Get the content length value if within a sensible range. + if (strlen(value) > 7) { + return SmartPointer(); + } + for (int i = 0; value[i] != '\0'; i++) { + // Bail out if illegal data. + if (value[i] < '0' || value[i] > '9') { + return SmartPointer(); + } + content_length = 10 * content_length + (value[i] - '0'); + } + } else { + // For now just print all other headers than Content-Length. + PrintF("%s: %s\n", key, value != NULL ? value : "(no value)"); + } } // Return now if no body. @@ -298,34 +295,34 @@ bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, // Send the header. len = OS::SNPrintF(Vector(buffer, kBufferSize), - "Type: connect\n"); + "Type: connect\r\n"); ok = conn->Send(buffer, len); if (!ok) return false; len = OS::SNPrintF(Vector(buffer, kBufferSize), - "V8-Version: %s\n", v8::V8::GetVersion()); + "V8-Version: %s\r\n", v8::V8::GetVersion()); ok = conn->Send(buffer, len); if (!ok) return false; len = OS::SNPrintF(Vector(buffer, kBufferSize), - "Protocol-Version: 1\n"); + "Protocol-Version: 1\r\n"); ok = conn->Send(buffer, len); if (!ok) return false; if (embedding_host != NULL) { len = OS::SNPrintF(Vector(buffer, kBufferSize), - "Embedding-Host: %s\n", embedding_host); + "Embedding-Host: %s\r\n", embedding_host); ok = conn->Send(buffer, len); if (!ok) return false; } len = OS::SNPrintF(Vector(buffer, kBufferSize), - "%s: 0\n", kContentLength); + "%s: 0\r\n", kContentLength); ok = conn->Send(buffer, len); if (!ok) return false; // Terminate header with empty line. - len = OS::SNPrintF(Vector(buffer, kBufferSize), "\n"); + len = OS::SNPrintF(Vector(buffer, kBufferSize), "\r\n"); ok = conn->Send(buffer, len); if (!ok) return false; @@ -349,11 +346,11 @@ bool DebuggerAgentUtil::SendMessage(const Socket* conn, // Send the header. int len; len = OS::SNPrintF(Vector(buffer, kBufferSize), - "%s: %d\n", kContentLength, utf8_len); + "%s: %d\r\n", kContentLength, utf8_len); conn->Send(buffer, len); // Terminate header with empty line. - len = OS::SNPrintF(Vector(buffer, kBufferSize), "\n"); + len = OS::SNPrintF(Vector(buffer, kBufferSize), "\r\n"); conn->Send(buffer, len); // Send message body as UTF-8. @@ -386,11 +383,11 @@ bool DebuggerAgentUtil::SendMessage(const Socket* conn, // Send the header. int len; len = OS::SNPrintF(Vector(buffer, kBufferSize), - "Content-Length: %d\n", utf8_request.length()); + "Content-Length: %d\r\n", utf8_request.length()); conn->Send(buffer, len); // Terminate header with empty line. - len = OS::SNPrintF(Vector(buffer, kBufferSize), "\n"); + len = OS::SNPrintF(Vector(buffer, kBufferSize), "\r\n"); conn->Send(buffer, len); // Send message body as UTF-8.