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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // Close existing session if any. | 89 // Close existing session if any. |
90 CloseSession(); | 90 CloseSession(); |
91 } | 91 } |
92 | 92 |
93 | 93 |
94 void DebuggerAgent::CreateSession(Socket* client) { | 94 void DebuggerAgent::CreateSession(Socket* client) { |
95 ScopedLock with(session_access_); | 95 ScopedLock with(session_access_); |
96 | 96 |
97 // If another session is already established terminate this one. | 97 // If another session is already established terminate this one. |
98 if (session_ != NULL) { | 98 if (session_ != NULL) { |
99 static const char* message = "Remote debugging session already active\n"; | 99 static const char* message = "Remote debugging session already active\r\n"; |
100 | 100 |
101 client->Send(message, strlen(message)); | 101 client->Send(message, strlen(message)); |
102 delete client; | 102 delete client; |
103 return; | 103 return; |
104 } | 104 } |
105 | 105 |
106 // Create a new session and hook up the debug message handler. | 106 // Create a new session and hook up the debug message handler. |
107 session_ = new DebuggerAgentSession(this, client); | 107 session_ = new DebuggerAgentSession(this, client); |
108 v8::Debug::SetMessageHandler(DebuggerAgentMessageHandler, this); | 108 v8::Debug::SetMessageHandler(DebuggerAgentMessageHandler, this); |
109 session_->Start(); | 109 session_->Start(); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 195 |
196 | 196 |
197 const char* DebuggerAgentUtil::kContentLength = "Content-Length"; | 197 const char* DebuggerAgentUtil::kContentLength = "Content-Length"; |
198 int DebuggerAgentUtil::kContentLengthSize = strlen(kContentLength); | 198 int DebuggerAgentUtil::kContentLengthSize = strlen(kContentLength); |
199 | 199 |
200 | 200 |
201 SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { | 201 SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { |
202 int received; | 202 int received; |
203 | 203 |
204 // Read header. | 204 // Read header. |
205 const int kHeaderBufferSize = 80; | |
206 char header_buffer[kHeaderBufferSize]; | |
207 int header_buffer_position = 0; | |
208 char c = '\0'; // One character receive buffer. | |
209 char last_c = '\0'; // Previous character. | |
210 int content_length = 0; | 205 int content_length = 0; |
211 while (!(c == '\n' && last_c == '\n')) { | 206 while (true) { |
212 last_c = c; | 207 const int kHeaderBufferSize = 80; |
213 received = conn->Receive(&c, 1); | 208 char header_buffer[kHeaderBufferSize]; |
214 if (received <= 0) { | 209 int header_buffer_position = 0; |
215 PrintF("Error %d\n", Socket::LastError()); | 210 char c = '\0'; // One character receive buffer. |
216 return SmartPointer<char>(); | 211 char prev_c = '\0'; // Previous character. |
217 } | |
218 | 212 |
219 // Check for end of header line. | 213 // Read until CRLF. |
220 if (c == '\n') { | 214 while (!(c == '\n' && prev_c == '\r')) { |
221 // Empty header line. | 215 prev_c = c; |
222 if (header_buffer_position == 0) { | 216 received = conn->Receive(&c, 1); |
223 continue; | 217 if (received <= 0) { |
| 218 PrintF("Error %d\n", Socket::LastError()); |
| 219 return SmartPointer<char>(); |
224 } | 220 } |
225 | 221 |
226 // Terminate header. | 222 // Add character to header buffer. |
227 ASSERT(header_buffer_position < kHeaderBufferSize); | |
228 if (header_buffer_position < kHeaderBufferSize) { | 223 if (header_buffer_position < kHeaderBufferSize) { |
229 header_buffer[header_buffer_position] = '\0'; | |
230 } | |
231 | |
232 // Split header. | |
233 char* key = header_buffer; | |
234 char* value = NULL; | |
235 for (int i = 0; i < header_buffer_position; i++) { | |
236 if (header_buffer[i] == ':') { | |
237 header_buffer[i] = '\0'; | |
238 value = header_buffer + i + 1; | |
239 while (*value == ' ') { | |
240 value++; | |
241 } | |
242 break; | |
243 } | |
244 } | |
245 | |
246 // Check that key is Content-Length. | |
247 if (strcmp(key, kContentLength) == 0) { | |
248 // Get the content length value if within a sensible range. | |
249 if (strlen(value) > 7) { | |
250 return SmartPointer<char>(); | |
251 } | |
252 for (int i = 0; value[i] != '\0'; i++) { | |
253 // Bail out if illegal data. | |
254 if (value[i] < '0' || value[i] > '9') { | |
255 return SmartPointer<char>(); | |
256 } | |
257 content_length = 10 * content_length + (value[i] - '0'); | |
258 } | |
259 } else { | |
260 // For now just print all other headers than Content-Length. | |
261 PrintF("%s: %s\n", key, value); | |
262 } | |
263 | |
264 // Start collecting new header. | |
265 header_buffer_position = 0; | |
266 } else { | |
267 // Add character to header buffer (reserve room for terminating '\0'). | |
268 if (header_buffer_position < kHeaderBufferSize - 1) { | |
269 header_buffer[header_buffer_position++] = c; | 224 header_buffer[header_buffer_position++] = c; |
270 } | 225 } |
271 } | 226 } |
| 227 |
| 228 // Check for end of header (empty header line). |
| 229 if (header_buffer_position == 2) { // Receive buffer contains CRLF. |
| 230 break; |
| 231 } |
| 232 |
| 233 // Terminate header. |
| 234 ASSERT(header_buffer_position > 1); // At least CRLF is received. |
| 235 ASSERT(header_buffer_position <= kHeaderBufferSize); |
| 236 header_buffer[header_buffer_position - 2] = '\0'; |
| 237 |
| 238 // Split header. |
| 239 char* key = header_buffer; |
| 240 char* value = NULL; |
| 241 for (int i = 0; header_buffer[i] != '\0'; i++) { |
| 242 if (header_buffer[i] == ':') { |
| 243 header_buffer[i] = '\0'; |
| 244 value = header_buffer + i + 1; |
| 245 while (*value == ' ') { |
| 246 value++; |
| 247 } |
| 248 break; |
| 249 } |
| 250 } |
| 251 |
| 252 // Check that key is Content-Length. |
| 253 if (strcmp(key, kContentLength) == 0) { |
| 254 // Get the content length value if within a sensible range. |
| 255 if (strlen(value) > 7) { |
| 256 return SmartPointer<char>(); |
| 257 } |
| 258 for (int i = 0; value[i] != '\0'; i++) { |
| 259 // Bail out if illegal data. |
| 260 if (value[i] < '0' || value[i] > '9') { |
| 261 return SmartPointer<char>(); |
| 262 } |
| 263 content_length = 10 * content_length + (value[i] - '0'); |
| 264 } |
| 265 } else { |
| 266 // For now just print all other headers than Content-Length. |
| 267 PrintF("%s: %s\n", key, value != NULL ? value : "(no value)"); |
| 268 } |
272 } | 269 } |
273 | 270 |
274 // Return now if no body. | 271 // Return now if no body. |
275 if (content_length == 0) { | 272 if (content_length == 0) { |
276 return SmartPointer<char>(); | 273 return SmartPointer<char>(); |
277 } | 274 } |
278 | 275 |
279 // Read body. | 276 // Read body. |
280 char* buffer = NewArray<char>(content_length + 1); | 277 char* buffer = NewArray<char>(content_length + 1); |
281 received = ReceiveAll(conn, buffer, content_length); | 278 received = ReceiveAll(conn, buffer, content_length); |
282 if (received < content_length) { | 279 if (received < content_length) { |
283 PrintF("Error %d\n", Socket::LastError()); | 280 PrintF("Error %d\n", Socket::LastError()); |
284 return SmartPointer<char>(); | 281 return SmartPointer<char>(); |
285 } | 282 } |
286 buffer[content_length] = '\0'; | 283 buffer[content_length] = '\0'; |
287 | 284 |
288 return SmartPointer<char>(buffer); | 285 return SmartPointer<char>(buffer); |
289 } | 286 } |
290 | 287 |
291 | 288 |
292 bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, | 289 bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, |
293 const char* embedding_host) { | 290 const char* embedding_host) { |
294 static const int kBufferSize = 80; | 291 static const int kBufferSize = 80; |
295 char buffer[kBufferSize]; // Sending buffer. | 292 char buffer[kBufferSize]; // Sending buffer. |
296 bool ok; | 293 bool ok; |
297 int len; | 294 int len; |
298 | 295 |
299 // Send the header. | 296 // Send the header. |
300 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 297 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
301 "Type: connect\n"); | 298 "Type: connect\r\n"); |
302 ok = conn->Send(buffer, len); | 299 ok = conn->Send(buffer, len); |
303 if (!ok) return false; | 300 if (!ok) return false; |
304 | 301 |
305 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 302 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
306 "V8-Version: %s\n", v8::V8::GetVersion()); | 303 "V8-Version: %s\r\n", v8::V8::GetVersion()); |
307 ok = conn->Send(buffer, len); | 304 ok = conn->Send(buffer, len); |
308 if (!ok) return false; | 305 if (!ok) return false; |
309 | 306 |
310 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 307 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
311 "Protocol-Version: 1\n"); | 308 "Protocol-Version: 1\r\n"); |
312 ok = conn->Send(buffer, len); | 309 ok = conn->Send(buffer, len); |
313 if (!ok) return false; | 310 if (!ok) return false; |
314 | 311 |
315 if (embedding_host != NULL) { | 312 if (embedding_host != NULL) { |
316 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 313 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
317 "Embedding-Host: %s\n", embedding_host); | 314 "Embedding-Host: %s\r\n", embedding_host); |
318 ok = conn->Send(buffer, len); | 315 ok = conn->Send(buffer, len); |
319 if (!ok) return false; | 316 if (!ok) return false; |
320 } | 317 } |
321 | 318 |
322 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 319 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
323 "%s: 0\n", kContentLength); | 320 "%s: 0\r\n", kContentLength); |
324 ok = conn->Send(buffer, len); | 321 ok = conn->Send(buffer, len); |
325 if (!ok) return false; | 322 if (!ok) return false; |
326 | 323 |
327 // Terminate header with empty line. | 324 // Terminate header with empty line. |
328 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); | 325 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
329 ok = conn->Send(buffer, len); | 326 ok = conn->Send(buffer, len); |
330 if (!ok) return false; | 327 if (!ok) return false; |
331 | 328 |
332 // No body for connect message. | 329 // No body for connect message. |
333 | 330 |
334 return true; | 331 return true; |
335 } | 332 } |
336 | 333 |
337 | 334 |
338 bool DebuggerAgentUtil::SendMessage(const Socket* conn, | 335 bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
339 const Vector<uint16_t> message) { | 336 const Vector<uint16_t> message) { |
340 static const int kBufferSize = 80; | 337 static const int kBufferSize = 80; |
341 char buffer[kBufferSize]; // Sending buffer both for header and body. | 338 char buffer[kBufferSize]; // Sending buffer both for header and body. |
342 | 339 |
343 // Calculate the message size in UTF-8 encoding. | 340 // Calculate the message size in UTF-8 encoding. |
344 int utf8_len = 0; | 341 int utf8_len = 0; |
345 for (int i = 0; i < message.length(); i++) { | 342 for (int i = 0; i < message.length(); i++) { |
346 utf8_len += unibrow::Utf8::Length(message[i]); | 343 utf8_len += unibrow::Utf8::Length(message[i]); |
347 } | 344 } |
348 | 345 |
349 // Send the header. | 346 // Send the header. |
350 int len; | 347 int len; |
351 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 348 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
352 "%s: %d\n", kContentLength, utf8_len); | 349 "%s: %d\r\n", kContentLength, utf8_len); |
353 conn->Send(buffer, len); | 350 conn->Send(buffer, len); |
354 | 351 |
355 // Terminate header with empty line. | 352 // Terminate header with empty line. |
356 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); | 353 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
357 conn->Send(buffer, len); | 354 conn->Send(buffer, len); |
358 | 355 |
359 // Send message body as UTF-8. | 356 // Send message body as UTF-8. |
360 int buffer_position = 0; // Current buffer position. | 357 int buffer_position = 0; // Current buffer position. |
361 for (int i = 0; i < message.length(); i++) { | 358 for (int i = 0; i < message.length(); i++) { |
362 // Write next UTF-8 encoded character to buffer. | 359 // Write next UTF-8 encoded character to buffer. |
363 buffer_position += | 360 buffer_position += |
364 unibrow::Utf8::Encode(buffer + buffer_position, message[i]); | 361 unibrow::Utf8::Encode(buffer + buffer_position, message[i]); |
365 ASSERT(buffer_position < kBufferSize); | 362 ASSERT(buffer_position < kBufferSize); |
366 | 363 |
(...skipping 12 matching lines...) Expand all Loading... |
379 const v8::Handle<v8::String> request) { | 376 const v8::Handle<v8::String> request) { |
380 static const int kBufferSize = 80; | 377 static const int kBufferSize = 80; |
381 char buffer[kBufferSize]; // Sending buffer both for header and body. | 378 char buffer[kBufferSize]; // Sending buffer both for header and body. |
382 | 379 |
383 // Convert the request to UTF-8 encoding. | 380 // Convert the request to UTF-8 encoding. |
384 v8::String::Utf8Value utf8_request(request); | 381 v8::String::Utf8Value utf8_request(request); |
385 | 382 |
386 // Send the header. | 383 // Send the header. |
387 int len; | 384 int len; |
388 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 385 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
389 "Content-Length: %d\n", utf8_request.length()); | 386 "Content-Length: %d\r\n", utf8_request.length()); |
390 conn->Send(buffer, len); | 387 conn->Send(buffer, len); |
391 | 388 |
392 // Terminate header with empty line. | 389 // Terminate header with empty line. |
393 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); | 390 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
394 conn->Send(buffer, len); | 391 conn->Send(buffer, len); |
395 | 392 |
396 // Send message body as UTF-8. | 393 // Send message body as UTF-8. |
397 conn->Send(*utf8_request, utf8_request.length()); | 394 conn->Send(*utf8_request, utf8_request.length()); |
398 | 395 |
399 return true; | 396 return true; |
400 } | 397 } |
401 | 398 |
402 | 399 |
403 // Receive the full buffer before returning unless an error occours. | 400 // Receive the full buffer before returning unless an error occours. |
404 int DebuggerAgentUtil::ReceiveAll(const Socket* conn, char* data, int len) { | 401 int DebuggerAgentUtil::ReceiveAll(const Socket* conn, char* data, int len) { |
405 int total_received = 0; | 402 int total_received = 0; |
406 while (total_received < len) { | 403 while (total_received < len) { |
407 int received = conn->Receive(data + total_received, len - total_received); | 404 int received = conn->Receive(data + total_received, len - total_received); |
408 if (received <= 0) { | 405 if (received <= 0) { |
409 return total_received; | 406 return total_received; |
410 } | 407 } |
411 total_received += received; | 408 total_received += received; |
412 } | 409 } |
413 return total_received; | 410 return total_received; |
414 } | 411 } |
415 | 412 |
416 | 413 |
417 } } // namespace v8::internal | 414 } } // namespace v8::internal |
OLD | NEW |