OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 162 } |
163 | 163 |
164 | 164 |
165 void DebuggerAgentSession::Run() { | 165 void DebuggerAgentSession::Run() { |
166 // Send the hello message. | 166 // Send the hello message. |
167 bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_); | 167 bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_); |
168 if (!ok) return; | 168 if (!ok) return; |
169 | 169 |
170 while (true) { | 170 while (true) { |
171 // Read data from the debugger front end. | 171 // Read data from the debugger front end. |
172 SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_); | 172 SmartArrayPointer<char> message = |
| 173 DebuggerAgentUtil::ReceiveMessage(client_); |
173 | 174 |
174 const char* msg = *message; | 175 const char* msg = *message; |
175 bool is_closing_session = (msg == NULL); | 176 bool is_closing_session = (msg == NULL); |
176 | 177 |
177 if (msg == NULL) { | 178 if (msg == NULL) { |
178 // If we lost the connection, then simulate a disconnect msg: | 179 // If we lost the connection, then simulate a disconnect msg: |
179 msg = "{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}"; | 180 msg = "{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}"; |
180 | 181 |
181 } else { | 182 } else { |
182 // Check if we're getting a disconnect request: | 183 // Check if we're getting a disconnect request: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // Shutdown the socket to end the blocking receive. | 226 // Shutdown the socket to end the blocking receive. |
226 client_->Shutdown(); | 227 client_->Shutdown(); |
227 } | 228 } |
228 | 229 |
229 | 230 |
230 const char* const DebuggerAgentUtil::kContentLength = "Content-Length"; | 231 const char* const DebuggerAgentUtil::kContentLength = "Content-Length"; |
231 const int DebuggerAgentUtil::kContentLengthSize = | 232 const int DebuggerAgentUtil::kContentLengthSize = |
232 StrLength(kContentLength); | 233 StrLength(kContentLength); |
233 | 234 |
234 | 235 |
235 SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { | 236 SmartArrayPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { |
236 int received; | 237 int received; |
237 | 238 |
238 // Read header. | 239 // Read header. |
239 int content_length = 0; | 240 int content_length = 0; |
240 while (true) { | 241 while (true) { |
241 const int kHeaderBufferSize = 80; | 242 const int kHeaderBufferSize = 80; |
242 char header_buffer[kHeaderBufferSize]; | 243 char header_buffer[kHeaderBufferSize]; |
243 int header_buffer_position = 0; | 244 int header_buffer_position = 0; |
244 char c = '\0'; // One character receive buffer. | 245 char c = '\0'; // One character receive buffer. |
245 char prev_c = '\0'; // Previous character. | 246 char prev_c = '\0'; // Previous character. |
246 | 247 |
247 // Read until CRLF. | 248 // Read until CRLF. |
248 while (!(c == '\n' && prev_c == '\r')) { | 249 while (!(c == '\n' && prev_c == '\r')) { |
249 prev_c = c; | 250 prev_c = c; |
250 received = conn->Receive(&c, 1); | 251 received = conn->Receive(&c, 1); |
251 if (received <= 0) { | 252 if (received <= 0) { |
252 PrintF("Error %d\n", Socket::LastError()); | 253 PrintF("Error %d\n", Socket::LastError()); |
253 return SmartPointer<char>(); | 254 return SmartArrayPointer<char>(); |
254 } | 255 } |
255 | 256 |
256 // Add character to header buffer. | 257 // Add character to header buffer. |
257 if (header_buffer_position < kHeaderBufferSize) { | 258 if (header_buffer_position < kHeaderBufferSize) { |
258 header_buffer[header_buffer_position++] = c; | 259 header_buffer[header_buffer_position++] = c; |
259 } | 260 } |
260 } | 261 } |
261 | 262 |
262 // Check for end of header (empty header line). | 263 // Check for end of header (empty header line). |
263 if (header_buffer_position == 2) { // Receive buffer contains CRLF. | 264 if (header_buffer_position == 2) { // Receive buffer contains CRLF. |
(...skipping 16 matching lines...) Expand all Loading... |
280 value++; | 281 value++; |
281 } | 282 } |
282 break; | 283 break; |
283 } | 284 } |
284 } | 285 } |
285 | 286 |
286 // Check that key is Content-Length. | 287 // Check that key is Content-Length. |
287 if (strcmp(key, kContentLength) == 0) { | 288 if (strcmp(key, kContentLength) == 0) { |
288 // Get the content length value if present and within a sensible range. | 289 // Get the content length value if present and within a sensible range. |
289 if (value == NULL || strlen(value) > 7) { | 290 if (value == NULL || strlen(value) > 7) { |
290 return SmartPointer<char>(); | 291 return SmartArrayPointer<char>(); |
291 } | 292 } |
292 for (int i = 0; value[i] != '\0'; i++) { | 293 for (int i = 0; value[i] != '\0'; i++) { |
293 // Bail out if illegal data. | 294 // Bail out if illegal data. |
294 if (value[i] < '0' || value[i] > '9') { | 295 if (value[i] < '0' || value[i] > '9') { |
295 return SmartPointer<char>(); | 296 return SmartArrayPointer<char>(); |
296 } | 297 } |
297 content_length = 10 * content_length + (value[i] - '0'); | 298 content_length = 10 * content_length + (value[i] - '0'); |
298 } | 299 } |
299 } else { | 300 } else { |
300 // For now just print all other headers than Content-Length. | 301 // For now just print all other headers than Content-Length. |
301 PrintF("%s: %s\n", key, value != NULL ? value : "(no value)"); | 302 PrintF("%s: %s\n", key, value != NULL ? value : "(no value)"); |
302 } | 303 } |
303 } | 304 } |
304 | 305 |
305 // Return now if no body. | 306 // Return now if no body. |
306 if (content_length == 0) { | 307 if (content_length == 0) { |
307 return SmartPointer<char>(); | 308 return SmartArrayPointer<char>(); |
308 } | 309 } |
309 | 310 |
310 // Read body. | 311 // Read body. |
311 char* buffer = NewArray<char>(content_length + 1); | 312 char* buffer = NewArray<char>(content_length + 1); |
312 received = ReceiveAll(conn, buffer, content_length); | 313 received = ReceiveAll(conn, buffer, content_length); |
313 if (received < content_length) { | 314 if (received < content_length) { |
314 PrintF("Error %d\n", Socket::LastError()); | 315 PrintF("Error %d\n", Socket::LastError()); |
315 return SmartPointer<char>(); | 316 return SmartArrayPointer<char>(); |
316 } | 317 } |
317 buffer[content_length] = '\0'; | 318 buffer[content_length] = '\0'; |
318 | 319 |
319 return SmartPointer<char>(buffer); | 320 return SmartArrayPointer<char>(buffer); |
320 } | 321 } |
321 | 322 |
322 | 323 |
323 bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, | 324 bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, |
324 const char* embedding_host) { | 325 const char* embedding_host) { |
325 static const int kBufferSize = 80; | 326 static const int kBufferSize = 80; |
326 char buffer[kBufferSize]; // Sending buffer. | 327 char buffer[kBufferSize]; // Sending buffer. |
327 bool ok; | 328 bool ok; |
328 int len; | 329 int len; |
329 | 330 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 return total_received; | 441 return total_received; |
441 } | 442 } |
442 total_received += received; | 443 total_received += received; |
443 } | 444 } |
444 return total_received; | 445 return total_received; |
445 } | 446 } |
446 | 447 |
447 } } // namespace v8::internal | 448 } } // namespace v8::internal |
448 | 449 |
449 #endif // ENABLE_DEBUGGER_SUPPORT | 450 #endif // ENABLE_DEBUGGER_SUPPORT |
OLD | NEW |