unit: OverbyteIcsHttpProt.pas
procedure THttpCli.SendRequest(const Method, Version: String);
var
Headers : TStrings;
N : Integer;
begin
{$IFDEF UseBandwidthControl}
FBandwidthCount := 0; // Reset byte counter
if httpoBandwidthControl in FOptions then begin
if not Assigned(FBandwidthTimer) then
FBandwidthTimer := TIcsTimer.Create(Self);
FBandwidthTimer.Enabled := FALSE;
FBandwidthTimer.Interval := FBandwidthSampling;
FBandwidthTimer.OnTimer := BandwidthTimerTimer;
FBandwidthTimer.Enabled := TRUE;
// Number of bytes we allow during a sampling period
FBandwidthMaxCount := Int64(FBandwidthLimit) * FBandwidthSampling div 1000;
FBandwidthPaused := FALSE;
FCtrlSocket.ComponentOptions := FCtrlSocket.ComponentOptions + [wsoNoReceiveLoop];
end;
{$ENDIF}
Headers := TStringList.Create;
try
FReqStream.Clear;
TriggerRequestHeaderBegin;
{* OutputDebugString(method + ' ' + FPath + ' HTTP/' + Version); *}
if Method = 'CONNECT' then
Headers.Add(Method + ' ' + FTargetHost + ':' + FTargetPort +
' HTTP/' + Version)
else begin
Headers.Add(method + ' ' + FPath + ' HTTP/' + Version);
if FSender <> '' then
Headers.Add('From: ' + FSender);
if FAccept <> '' then
Headers.Add('Accept: ' + FAccept);
if FReference <> '' then
Headers.Add('Referer: ' + FReference);
if FCurrConnection <> '' then
Headers.Add('Connection: ' + FCurrConnection);
if FAcceptLanguage <> '' then
Headers.Add('Accept-Language: ' + FAcceptLanguage);
{$IFDEF UseContentCoding}
if (FContentCodingHnd.HeaderText <> '') and (FRequestType <> httpHEAD) then
Headers.Add('Accept-Encoding: ' + FContentCodingHnd.HeaderText);
{$ENDIF}
if (FRequestType in [httpPOST, httpPUT, httpPATCH]) and { V8.06 }
(FContentPost <> '') then
Headers.Add('Content-Type: ' + FContentPost);
{if ((method = 'PUT') or (method = 'POST')) and (FContentPost <> '') then
Headers.Add('Content-Type: ' + FContentPost);}
end;
if FAgent <> '' then
Headers.Add('User-Agent: ' + FAgent);
if (FTargetPort = '80') or (FTargetPort = '443') or (FTargetPort = '') then { V8.05 }
Headers.Add('Host: ' + FTargetHost)
else
Headers.Add('Host: ' + FTargetHost + ':' + FTargetPort);
if FNoCache then
Headers.Add('Pragma: no-cache');
if FCurrProxyConnection <> '' then
Headers.Add('Proxy-Connection: ' + FCurrProxyConnection);
if (Method = 'CONNECT') then // <= 12/29/05 AG
Headers.Add('Content-Length: 0') // <= 12/29/05 AG}
else begin { V7.05 begin }
if FRequestType in [httpPOST, httpPUT, httpPATCH] then begin { V8.06 }
{$IFDEF UseNTLMAuthentication}
if (FAuthNTLMState = ntlmMsg1) or
(FProxyAuthNTLMState = ntlmMsg1) then
Headers.Add('Content-Length: 0')
else
{$ENDIF}
Headers.Add('Content-Length: ' +
IntToStr(SendStream.Size - SendStream.Position));
end
{gavin.chen 2014.05.07 ++++++ 加了对于 delete 参数的,content length}
else if (FRequestType = httpDELETE) AND (Assigned(SendStream)) then
begin
Headers.Add('Content-Length: ' +
IntToStr(SendStream.Size - SendStream.Position));
end;
{+++++++++++++++++++++}
end; { V7.05 end }
{ if (method = 'PUT') or (method = 'POST') then
Headers.Add('Content-Length: ' + IntToStr(SendStream.Size));}
if FModifiedSince <> 0 then
Headers.Add('If-Modified-Since: ' +
RFC1123_Date(FModifiedSince) + ' GMT');
if not FProxyConnected then begin
{ We did not call SetReady while in relocation, adjust }
{ ProxyAuthNtlmState if we got disconnected from proxy }
{$IFDEF UseNTLMAuthentication}
if FProxyAuthNtlmState = ntlmDone then
FProxyAuthNtlmState := ntlmMsg1;
{$ENDIF}
end;
{$IFDEF UseNTLMAuthentication}
if (FProxyAuthNTLMState <> ntlmMsg1) then begin
if (FAuthNTLMState = ntlmMsg1) then
Headers.Add(GetNTLMMessage1(FALSE))
else if (FAuthNTLMState = ntlmMsg3) then
Headers.Add(GetNTLMMessage3(Method, FALSE))
{$IFDEF UseDigestAuthentication}
else if (FAuthDigestState = digestMsg1) then
Headers.Add(GetDigestAuthorizationHeader(Method, FALSE))
{$ENDIF}
else if (FAuthBasicState = basicMsg1) then
Headers.Add(GetBasicAuthorizationHeader(Method, FALSE))
else begin
// Maybe an event to add a preemptive Authorization header?
end;
end;
{$ELSE}
{$IFDEF UseDigestAuthentication}
if (FAuthDigestState = digestMsg1) then
Headers.Add(GetDigestAuthorizationHeader(Method, FALSE))
else
{$ENDIF}
if (FAuthBasicState = basicMsg1) then
Headers.Add(GetBasicAuthorizationHeader(Method, FALSE))
else begin
// Maybe an event to add a preemptive Authorization header?
end;
{$ENDIF}
{$IFDEF UseNTLMAuthentication}
if (FProxyAuthNTLMState = ntlmMsg1) then
Headers.Add(GetNTLMMessage1(TRUE))
else if (FProxyAuthNTLMState = ntlmMsg3) then
Headers.Add(GetNTLMMessage3(Method, TRUE))
else
{$ENDIF}
{$IFDEF UseDigestAuthentication}
if (FProxyAuthDigestState = digestMsg1) then
Headers.Add(GetDigestAuthorizationHeader(Method, TRUE))
else
{$ENDIF}
if (FProxyAuthBasicState = basicMsg1) then
Headers.Add(GetBasicAuthorizationHeader(Method, TRUE))
else if Length(FProxy) > 0 then begin
// Maybe an event to add a preemptive Authorization header?
end;
if FCookie <> '' then
Headers.Add('Cookie: ' + FCookie);
if (FContentRangeBegin <> '') or (FContentRangeEnd <> '') then begin {JMR!! Added this line!!!}
Headers.Add('Range: bytes=' + FContentRangeBegin + '-' + FContentRangeEnd); {JMR!! Added this line!!!}
if (Method <> 'CONNECT') then { V7.21 }
begin
FContentRangeBegin := ''; {JMR!! Added this line!!!}
FContentRangeEnd := ''; {JMR!! Added this line!!!}
end;
end; {JMR!! Added this line!!!}
FAcceptRanges := '';
{SendCommand('UA-pixels: 1024x768'); }
{SendCommand('UA-color: color8'); }
{SendCommand('UA-OS: Windows 95'); }
{SendCommand('UA-CPU: x86'); }
{SendCommand('Proxy-Connection: Keep-Alive'); }
{$IFNDEF NO_DEBUG_LOG}
if CheckLogOptions(loProtSpecInfo) then { V1.91 } { replaces $IFDEF DEBUG_OUTPUT }
DebugLog(loProtSpecInfo, IntToStr(Headers.Count) +
' header lines to send'#13#10 + Headers.Text);
{$ENDIF}
TriggerBeforeHeaderSend(Method, Headers);
for N := 0 to Headers.Count - 1 do
SendCommand(Headers[N]);
TriggerRequestHeaderEnd;
SendCommand('');
FCtrlSocket.PutDataInSendBuffer(FReqStream.Memory, FReqStream.Size);
FReqStream.Clear;
FCtrlSocket.Send(nil, 0);
finally
Headers.Free;
{$IFNDEF NO_DEBUG_LOG}
if CheckLogOptions(loProtSpecInfo) then { V1.91 } { replaces $IFDEF DEBUG_OUTPUT }
DebugLog(loProtSpecInfo, 'SendRequest Done');
{$ENDIF}
end;
end;