Chris Pollett >
Students > [Bio] [Blog] |
Deliverable 2 - Implement HTTP 1.1 to HTTP/2 ConversionDescription: Implement a program that takes in an HTTP 1.1 request and converts it into HTTP/2 binary format. This program will give an understanding of how request conversion happens. The program works in 3 parts:
Example: GET /doc/test.html HTTP/1.1 Converts to :
[Frame 1] => 00 00 87 01 20 00 00 49 FF EB 6E 5E 9A 37 E4 83 D8 23 C5 A9 5D 73 C7 A3 2E DC B0 7B 04 8A 95 28 5F 8D D8 D7 5C 30 F4 D1 07 B0 5F 93 7E 3B FA 65 E7 D2 ED 1D 36 EC A9 1B F9 F4 83 D8 3B F7 EF 77 4C BC FA 63 86 37 63 DF B6 A0 E3 C7 97 87 48 3D 83 4E DC 39 F2 DF 9F 4E 6B 20 D3 B7 0E 7C B7 EA E1 97 3D 90 55 7D 55 0F 7E EE 99 77 74 B6 66 5D D9 FA 68 83 D8 33 D6 02 Step-1: Parse the requestThis is done by extracting all the parts of the request ('method', 'path', 'protocol', 'headers', 'body') and replacing them with the appropriate new format. The first function parses the request to extract out all parts and creates an array as follows:
returns:
array(
'method' => $method,
'path' => $path,
'protocol' => $protocol,
'headers' => $headers,
'body' => $body
);
Step-2: Setup frame creation configurationIn this step, the initial flags are set according to the parts of the request. The number and types of frames to be created are also decided within this function. Each frame to be created is then done by calling its respective function and passing the necessary config. Eg: a GET request with a body => 2 frames: Header (with end_header flag set) and data (with end_stream flag set)
Step-3: Creating HEADERS frameAccording to the new format, a request contains a certain number of headers with binary values assigned to them. These headers include packet type, stream identifier number, flags for priority, padding, etc. These headers are added to the existing HTTP headers, and a frame is created for them in the function:
returns:
Step-4: Creating DATA frameSince the example request had a body as well, the createDataFrame() is also triggered in step 2. This focuses on creating the frame format of the data frame and adds the body of the original request as this frame's payload.
returns:
NOTE: The fields in frames with value as “not set” are fields that are not mandatory. And according to the example get request we are following in this document, they are not required. These fields are ignored when converting to binary. Step-5: Convert to BinaryThe function convertFrameToBin($http2_frames) receives an array of frames as input. Within this function, each frame is iterated over, and its components are converted to binary format following the specifications outlined in the RFC. Binary values are assigned to the headers; for instance, the 'type' header may range from 0x0 to 0x9 based on the frame type. The field values of each header are then extracted and passed to respective functions responsible for converting them into binary values. These binary values are concatenated into a string. For instance, the function getTypeInBinary($type) handles the conversion process as outlined in the table below:
returns:
Step-6: Converting to HexadecimalConverting to hex is done by chunking the entire binary string into chunks of 8 bits each. These chunks are passed into a prebuilt function that converts binary to hex.
returns:
ResultsFind the full code below:parser.php.zip |