Skip to content

Commit

Permalink
fix: when HTTP response has no Content-Length
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Aug 22, 2021
1 parent 91aab75 commit 78f8f3f
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions HttpProxy/HttpToSocks5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,17 @@ public async ValueTask ForwardToSocks5Async(IDuplexPipe incomingPipe, Socks5Crea
var responseHeadersStr = await ReadHttpHeadersAsync(socks5Pipe.Input, cancellationToken);
_logger.LogDebug("{0} server headers received: \n{1}", LogHeader, responseHeadersStr);
var responseHeaders = ReadHeaders(responseHeadersStr);

incomingPipe.Output.Write(responseHeadersStr);
incomingPipe.Output.Write(HttpHeaderEnd);
await incomingPipe.Output.FlushAsync(cancellationToken);

if (IsChunked(responseHeaders))
{
incomingPipe.Output.Write(responseHeadersStr);
incomingPipe.Output.Write(HttpHeaderEnd);
await incomingPipe.Output.FlushAsync(cancellationToken);

await CopyChunksAsync(socks5Pipe.Input, incomingPipe.Output, cancellationToken);
}
else if (TryReadContentLength(responseHeaders, out var serverResponseContentLength))
{
incomingPipe.Output.Write(responseHeadersStr);
incomingPipe.Output.Write(HttpHeaderEnd);
await incomingPipe.Output.FlushAsync(cancellationToken);

if (serverResponseContentLength > 0)
{
_logger.LogDebug(@"{0} Waiting for up to {1} bytes from server", LogHeader, serverResponseContentLength);
Expand All @@ -106,10 +103,6 @@ public async ValueTask ForwardToSocks5Async(IDuplexPipe incomingPipe, Socks5Crea
_logger.LogDebug(@"{0} server sent {1} bytes to client", LogHeader, serverResponseContentLength);
}
}
else
{
await SendErrorAsync(incomingPipe.Output, ConnectionErrorResult.UnknownError, httpHeaders.HttpVersion, cancellationToken);
}
}
}

Expand Down

0 comments on commit 78f8f3f

Please sign in to comment.