Commit cf8c7cc3 authored by sgjesse@chromium.org's avatar sgjesse@chromium.org

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
parent 70aa1fde
...@@ -96,7 +96,7 @@ void DebuggerAgent::CreateSession(Socket* client) { ...@@ -96,7 +96,7 @@ void DebuggerAgent::CreateSession(Socket* client) {
// If another session is already established terminate this one. // If another session is already established terminate this one.
if (session_ != NULL) { 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)); client->Send(message, strlen(message));
delete client; delete client;
...@@ -202,37 +202,43 @@ SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { ...@@ -202,37 +202,43 @@ SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) {
int received; int received;
// Read header. // Read header.
int content_length = 0;
while (true) {
const int kHeaderBufferSize = 80; const int kHeaderBufferSize = 80;
char header_buffer[kHeaderBufferSize]; char header_buffer[kHeaderBufferSize];
int header_buffer_position = 0; int header_buffer_position = 0;
char c = '\0'; // One character receive buffer. char c = '\0'; // One character receive buffer.
char last_c = '\0'; // Previous character. char prev_c = '\0'; // Previous character.
int content_length = 0;
while (!(c == '\n' && last_c == '\n')) { // Read until CRLF.
last_c = c; while (!(c == '\n' && prev_c == '\r')) {
prev_c = c;
received = conn->Receive(&c, 1); received = conn->Receive(&c, 1);
if (received <= 0) { if (received <= 0) {
PrintF("Error %d\n", Socket::LastError()); PrintF("Error %d\n", Socket::LastError());
return SmartPointer<char>(); return SmartPointer<char>();
} }
// Check for end of header line. // Add character to header buffer.
if (c == '\n') { if (header_buffer_position < kHeaderBufferSize) {
// Empty header line. header_buffer[header_buffer_position++] = c;
if (header_buffer_position == 0) { }
continue;
} }
// Terminate header. // Check for end of header (empty header line).
ASSERT(header_buffer_position < kHeaderBufferSize); if (header_buffer_position == 2) { // Receive buffer contains CRLF.
if (header_buffer_position < kHeaderBufferSize) { break;
header_buffer[header_buffer_position] = '\0';
} }
// 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. // Split header.
char* key = header_buffer; char* key = header_buffer;
char* value = NULL; char* value = NULL;
for (int i = 0; i < header_buffer_position; i++) { for (int i = 0; header_buffer[i] != '\0'; i++) {
if (header_buffer[i] == ':') { if (header_buffer[i] == ':') {
header_buffer[i] = '\0'; header_buffer[i] = '\0';
value = header_buffer + i + 1; value = header_buffer + i + 1;
...@@ -258,16 +264,7 @@ SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { ...@@ -258,16 +264,7 @@ SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) {
} }
} else { } else {
// For now just print all other headers than Content-Length. // For now just print all other headers than Content-Length.
PrintF("%s: %s\n", key, value); PrintF("%s: %s\n", key, value != NULL ? value : "(no 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;
}
} }
} }
...@@ -298,34 +295,34 @@ bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, ...@@ -298,34 +295,34 @@ bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn,
// Send the header. // Send the header.
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"Type: connect\n"); "Type: connect\r\n");
ok = conn->Send(buffer, len); ok = conn->Send(buffer, len);
if (!ok) return false; if (!ok) return false;
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"V8-Version: %s\n", v8::V8::GetVersion()); "V8-Version: %s\r\n", v8::V8::GetVersion());
ok = conn->Send(buffer, len); ok = conn->Send(buffer, len);
if (!ok) return false; if (!ok) return false;
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"Protocol-Version: 1\n"); "Protocol-Version: 1\r\n");
ok = conn->Send(buffer, len); ok = conn->Send(buffer, len);
if (!ok) return false; if (!ok) return false;
if (embedding_host != NULL) { if (embedding_host != NULL) {
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"Embedding-Host: %s\n", embedding_host); "Embedding-Host: %s\r\n", embedding_host);
ok = conn->Send(buffer, len); ok = conn->Send(buffer, len);
if (!ok) return false; if (!ok) return false;
} }
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"%s: 0\n", kContentLength); "%s: 0\r\n", kContentLength);
ok = conn->Send(buffer, len); ok = conn->Send(buffer, len);
if (!ok) return false; if (!ok) return false;
// Terminate header with empty line. // Terminate header with empty line.
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n");
ok = conn->Send(buffer, len); ok = conn->Send(buffer, len);
if (!ok) return false; if (!ok) return false;
...@@ -349,11 +346,11 @@ bool DebuggerAgentUtil::SendMessage(const Socket* conn, ...@@ -349,11 +346,11 @@ bool DebuggerAgentUtil::SendMessage(const Socket* conn,
// Send the header. // Send the header.
int len; int len;
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"%s: %d\n", kContentLength, utf8_len); "%s: %d\r\n", kContentLength, utf8_len);
conn->Send(buffer, len); conn->Send(buffer, len);
// Terminate header with empty line. // Terminate header with empty line.
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n");
conn->Send(buffer, len); conn->Send(buffer, len);
// Send message body as UTF-8. // Send message body as UTF-8.
...@@ -386,11 +383,11 @@ bool DebuggerAgentUtil::SendMessage(const Socket* conn, ...@@ -386,11 +383,11 @@ bool DebuggerAgentUtil::SendMessage(const Socket* conn,
// Send the header. // Send the header.
int len; int len;
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"Content-Length: %d\n", utf8_request.length()); "Content-Length: %d\r\n", utf8_request.length());
conn->Send(buffer, len); conn->Send(buffer, len);
// Terminate header with empty line. // Terminate header with empty line.
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n");
conn->Send(buffer, len); conn->Send(buffer, len);
// Send message body as UTF-8. // Send message body as UTF-8.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment