HTTP Header not correctly loaded
## Summary TRequest class in unit HTTPDefs does not load all HTTP request fields. The HTTP Version is loaded with a random UUID. All the fields have a leading blank and custom fields are not loaded at all. The problem exists in all the platforms. ## System Information - Windows 11 x86-64 - Ubuntu 22.04.05 LTS x86-64 - FCP any version - HttpDefs.pp any version (also the current main trunk) ## Steps to reproduce Inherite TRequest class and add these methods: ``` THTTPRequest = class(TRequest) public function LoadContent(AStream: TStream): boolean; function LoadHeaders(AMethod: string; AHeaders: TStringList): boolean; function SetupRequest(): boolean; end; --- function THTTPRequest.LoadContent(AStream: TStream): boolean; var _Data: AnsiString; begin SetLength(_Data, AStream.Size); if AStream.Size > 0 then begin AStream.Seek(0, soFromBeginning); AStream.Read(_Data[1], AStream.Size); SetContentFromString(_Data); end; Result:= true; end; function THTTPRequest.LoadHeaders(AMethod: string; AHeaders: TStringList): boolean; begin Method:= AMethod; LoadFromStrings(AHeaders, true); Result:= True; end; function THTTPRequest.SetupRequest(): boolean; begin InitRequestVars(); PathInfo:= URI; Result:= true; end; ``` --- Prepare a string (or a file) with a normal HTTP request: ``` HTTP_REQUEST = 'POST /PostExample HTTP/1.1'+sLineBreak+ 'Content-Type: application/json'+sLineBreak+ 'Authorization: Bearer eyAidHlwIiA6ICJKV1QiLCAiYWxnIiA6ICJIUzI1NiIsICJraWQiIDogIjEHW3LuoK-Rmiqc'+sLineBreak+ 'User-Agent: PostmanRuntime/7.37.0'+sLineBreak+ 'Accept: */*'+sLineBreak+ 'Cache-Control: no-cache'+sLineBreak+ 'Postman-Token: 3483eb69-c296-45b3-b1e3-e7800bd375d2'+sLineBreak+ 'Host: 10.125.54.203:8000'+sLineBreak+ 'Accept-Encoding: gzip, deflate, br'+sLineBreak+ 'Connection: keep-alive'+sLineBreak+ 'Cookie: token=Alpha;param=Beta'+sLineBreak+ 'Content-Length: 143'+sLineBreak+ sLineBreak; CONTENT = '{ "modification_date": "2023-05-17T11:22:00.000001Z", "title": "Test 2" }'+sLineBreak; ``` and print the values: ``` procedure Test(); var _Headers: TStringList; _Request: THTTPRequest; begin _Headers:= TStringList.Create; _Headers.StrictDelimiter:= true; _Headers.AddText(HTTP_REQUEST); _Request:= THTTPRequest.Create; _Request.LoadHeaders('POST', _Headers); _Request.SetupRequest() ShowMessage('Content-Type: <'+_Request.ContentType+'>'+sLineBreak+ 'Cookie: <'+_Request.CookieFields.CommaText+'>'+sLineBreak+ 'HttpVersion: <'+_Request.HttpVersion+'>'+sLineBreak+ 'Cache-Control: <'+_Request.CustomHeaders.Values['Cache-Control']+'>'); end; ``` ## Example Project See attached file [HTTPDefsBug.zip](/uploads/6a905b642f6838645c1e1e325e76b251/HTTPDefsBug.zip) ## What is the current bug behavior? The dialog will show: ``` Content-Type: < application/json> Cookie: <" token=Alpha";param=Beta> HttpVersion: < 3483eb69-c296-45b3-b1e3-e7800bd735d2> Cache-Control: < > ``` ## What is the expected (correct) behavior? ``` Content-Type: <application/json> Cookie: <token=Alpha;param=Beta> HttpVersion: <1.1> Cache-Control: <no-cache> ``` ## Relevant logs and/or screenshots ## Possible fixes In the unit **HTTPDefs** in method **THTTPHeader.LoadFromStrings** the function P:=GetFieldNameIndex(VN) will never return -1. - Changed the check to 0 and added the SetCustomHeader. - Added trim to remove leading blanks. ``` 1920,1921c1920,1923 < If (P<>-1) then < SetFieldValue(P,S); --- > If (P<>0) then // Changed -1 in 0 > SetFieldValue(P,trim(S)) // Added TRIM > else // Added else > SetCustomHeader(VN, trim(S)); ``` Second fix for HTTPVersion: There is mixed use of private field FHttpVersion and property HttpVersion that desn't contain the same data. In method: **TRequest.ParseFirstHeaderLine** - Changed the HttpVersion property in FHttpVersion field - Set HttpVersion property according with the field ``` 2291c2293,2294 < FHttpVersion := Copy(HttpVersion, Pos('/', HttpVersion) + 1, Length(HttpVersion)); --- > FHttpVersion := Copy(FHttpVersion, Pos('/', FHttpVersion) + 1, Length(FHttpVersion)); // Changed HttpVersion in HttpVersion > HTTPVersion:= FHttpVersion; ``` The fixed file is attached too. /
issue