Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit b1e43e7

Browse files
justcoding121justcoding121
authored andcommitted
Add Expect100Continue disable option
For servers/clients having buggy implementation
1 parent 1922341 commit b1e43e7

File tree

6 files changed

+55
-152
lines changed

6 files changed

+55
-152
lines changed

Titanium.Web.Proxy/Helpers/Compression.cs

Lines changed: 0 additions & 139 deletions
This file was deleted.

Titanium.Web.Proxy/Network/HttpWebClient.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ public bool SendChunked
9797
}
9898
}
9999

100+
public bool ExpectContinue
101+
{
102+
get
103+
{
104+
var header = RequestHeaders.FirstOrDefault(x => x.Name.ToLower() == "expect");
105+
if (header != null) return header.Value.Equals("100-continue");
106+
return false;
107+
}
108+
}
109+
100110
public string Url { get { return RequestUri.OriginalString; } }
101111

102112
internal Encoding Encoding { get { return this.GetEncoding(); } }
@@ -127,7 +137,8 @@ internal bool UpgradeToWebSocket
127137
}
128138

129139
public List<HttpHeader> RequestHeaders { get; set; }
130-
140+
public bool Is100Continue { get; internal set; }
141+
public bool ExpectationFailed { get; internal set; }
131142

132143
public Request()
133144
{
@@ -251,6 +262,7 @@ internal bool IsChunked
251262
internal bool ResponseBodyRead { get; set; }
252263
internal bool ResponseLocked { get; set; }
253264
public bool Is100Continue { get; internal set; }
265+
public bool ExpectationFailed { get; internal set; }
254266

255267
public Response()
256268
{
@@ -311,6 +323,7 @@ public void SendRequest()
311323
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
312324
stream.Write(requestBytes, 0, requestBytes.Length);
313325
stream.Flush();
326+
314327
}
315328

316329
public void ReceiveResponse()
@@ -329,7 +342,7 @@ public void ReceiveResponse()
329342
this.Response.ResponseStatusCode = httpResult[1].Trim();
330343
this.Response.ResponseStatusDescription = httpResult[2].Trim();
331344

332-
if (this.Response.ResponseStatusCode.Equals("100")
345+
if (this.Response.ResponseStatusCode.Equals("100")
333346
&& this.Response.ResponseStatusDescription.ToLower().Equals("continue"))
334347
{
335348
this.Response.Is100Continue = true;
@@ -338,6 +351,13 @@ public void ReceiveResponse()
338351
ReceiveResponse();
339352
return;
340353
}
354+
{
355+
this.Response.ExpectationFailed = true;
356+
this.Response.ResponseStatusCode = null;
357+
ProxyClient.ServerStreamReader.ReadLine();
358+
ReceiveResponse();
359+
return;
360+
}
341361

342362
List<string> responseLines = ProxyClient.ServerStreamReader.ReadAllLines();
343363

Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public partial class ProxyServer
4040
#else
4141
internal static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Ssl3;
4242
#endif
43-
43+
4444
static ProxyServer()
4545
{
4646

@@ -56,6 +56,7 @@ static ProxyServer()
5656

5757
public static string RootCertificateIssuerName { get; set; }
5858
public static string RootCertificateName { get; set; }
59+
public static bool Enable100ContinueBehaviour { get; set; }
5960

6061
public static event EventHandler<SessionEventArgs> BeforeRequest;
6162
public static event EventHandler<SessionEventArgs> BeforeResponse;

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,21 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
266266
args.ProxySession.SetConnection(connection);
267267
args.ProxySession.SendRequest();
268268

269+
if(Enable100ContinueBehaviour)
270+
if (args.ProxySession.Request.Is100Continue)
271+
{
272+
WriteResponseStatus(args.ProxySession.Response.HttpVersion, "100",
273+
"Continue", args.Client.ClientStreamWriter);
274+
args.Client.ClientStreamWriter.WriteLine();
275+
}
276+
else if (args.ProxySession.Request.ExpectationFailed)
277+
{
278+
WriteResponseStatus(args.ProxySession.Response.HttpVersion, "417",
279+
"Expectation Failed", args.Client.ClientStreamWriter);
280+
args.Client.ClientStreamWriter.WriteLine();
281+
}
282+
283+
269284
//If request was modified by user
270285
if (args.ProxySession.Request.RequestBodyRead)
271286
{
@@ -275,14 +290,20 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
275290
}
276291
else
277292
{
278-
//If its a post/put request, then read the client html body and send it to server
279-
if (httpMethod.ToUpper() == "POST" || httpMethod.ToUpper() == "PUT")
293+
if (!args.ProxySession.Request.ExpectationFailed)
280294
{
281-
SendClientRequestBody(args);
295+
//If its a post/put request, then read the client html body and send it to server
296+
if (httpMethod.ToUpper() == "POST" || httpMethod.ToUpper() == "PUT")
297+
{
298+
SendClientRequestBody(args);
299+
}
282300
}
283301
}
284302

285-
HandleHttpSessionResponse(args);
303+
if (!args.ProxySession.Request.ExpectationFailed)
304+
{
305+
HandleHttpSessionResponse(args);
306+
}
286307

287308
//if connection is closing exit
288309
if (args.ProxySession.Response.ResponseKeepAlive == false)

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public static void HandleHttpSessionResponse(SessionEventArgs args)
4242
"Continue", args.Client.ClientStreamWriter);
4343
args.Client.ClientStreamWriter.WriteLine();
4444
}
45+
else if (args.ProxySession.Response.ExpectationFailed)
46+
{
47+
WriteResponseStatus(args.ProxySession.Response.HttpVersion, "417",
48+
"Expectation Failed", args.Client.ClientStreamWriter);
49+
args.Client.ClientStreamWriter.WriteLine();
50+
}
4551

4652
WriteResponseStatus(args.ProxySession.Response.HttpVersion, args.ProxySession.Response.ResponseStatusCode,
4753
args.ProxySession.Response.ResponseStatusDescription, args.Client.ClientStreamWriter);

Titanium.Web.Proxy/Responses/OkResponse.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ public OkResponse()
1010
{
1111
ResponseStatusCode = "200";
1212
ResponseStatusDescription = "Ok";
13-
14-
ResponseHeaders.Add(new HttpHeader("Timestamp", DateTime.Now.ToString()));
15-
ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
16-
ResponseHeaders.Add(new HttpHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
17-
ResponseHeaders.Add(new HttpHeader("Pragma", "no-cache"));
18-
ResponseHeaders.Add(new HttpHeader("Expires", "0"));
1913
}
2014
}
2115
}

0 commit comments

Comments
 (0)