Takes an HTTPRequest or HTTPResponse object, and creates its string representation.
(self, **kwargs)
| 417 | return pkt + pay |
| 418 | |
| 419 | def self_build(self, **kwargs): |
| 420 | """Takes an HTTPRequest or HTTPResponse object, and creates its |
| 421 | string representation.""" |
| 422 | if not isinstance(self.underlayer, HTTP): |
| 423 | warning("An HTTPResponse/HTTPRequest should always be below an HTTP") |
| 424 | # Check for cache |
| 425 | if self.raw_packet_cache is not None: |
| 426 | return self.raw_packet_cache |
| 427 | p = b"" |
| 428 | encodings = self._get_encodings() |
| 429 | # Walk all the fields, in order |
| 430 | for i, f in enumerate(self.fields_desc): |
| 431 | if f.name == "Unknown_Headers": |
| 432 | continue |
| 433 | # Get the field value |
| 434 | val = self.getfieldval(f.name) |
| 435 | if not val: |
| 436 | if f.name == "Content_Length" and "chunked" not in encodings: |
| 437 | # Add Content-Length anyways |
| 438 | val = str(len(self.payload or b"")) |
| 439 | elif f.name == "Date" and isinstance(self, HTTPResponse): |
| 440 | val = datetime.datetime.now(datetime.timezone.utc).strftime( |
| 441 | "%a, %d %b %Y %H:%M:%S GMT" |
| 442 | ) |
| 443 | else: |
| 444 | # Not specified. Skip |
| 445 | continue |
| 446 | |
| 447 | if i >= 3: |
| 448 | val = _header_line(f.real_name, val) |
| 449 | # Fields used in the first line have a space as a separator, |
| 450 | # whereas headers are terminated by a new line |
| 451 | if i <= 1: |
| 452 | separator = b" " |
| 453 | else: |
| 454 | separator = b"\r\n" |
| 455 | # Add the field into the packet |
| 456 | p = f.addfield(self, p, val + separator) |
| 457 | # Handle Unknown_Headers |
| 458 | if self.Unknown_Headers: |
| 459 | headers_text = b"" |
| 460 | for name, value in self.Unknown_Headers.items(): |
| 461 | headers_text += _header_line(name, value) + b"\r\n" |
| 462 | p = self.get_field("Unknown_Headers").addfield(self, p, headers_text) |
| 463 | # The packet might be empty, and in that case it should stay empty. |
| 464 | if p: |
| 465 | # Add an additional line after the last header |
| 466 | p = f.addfield(self, p, b"\r\n") |
| 467 | return p |
| 468 | |
| 469 | def guess_payload_class(self, payload): |
| 470 | """Detect potential payloads""" |
no test coverage detected