Skip to content

Commit f6fbca3

Browse files
committed
Fixed http clients
1 parent 1eb99b8 commit f6fbca3

File tree

9 files changed

+195
-25
lines changed

9 files changed

+195
-25
lines changed

examples/HttpServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected override void OnReceivedRequest(HttpRequest request)
9797
else if (request.Method == "OPTIONS")
9898
SendResponseAsync(Response.MakeOptionsResponse());
9999
else if (request.Method == "TRACE")
100-
SendResponseAsync(Response.MakeTraceResponse(request.Cache));
100+
SendResponseAsync(Response.MakeTraceResponse(request.Cache.Data));
101101
else
102102
SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
103103
}

source/NetCoreServer/HttpClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class HttpClient : TcpClient
5151
/// </summary>
5252
/// <param name="request">HTTP request</param>
5353
/// <returns>Size of sent data</returns>
54-
public long SendRequest(HttpRequest request) { return Send(request.Cache); }
54+
public long SendRequest(HttpRequest request) { return Send(request.Cache.Data, request.Cache.Offset, request.Cache.Size); }
5555

5656
/// <summary>
5757
/// Send the HTTP request body (synchronous)
@@ -84,7 +84,7 @@ public class HttpClient : TcpClient
8484
/// </summary>
8585
/// <param name="request">HTTP request</param>
8686
/// <returns>'true' if the current HTTP request was successfully sent, 'false' if the session is not connected</returns>
87-
public bool SendRequestAsync(HttpRequest request) { return SendAsync(request.Cache); }
87+
public bool SendRequestAsync(HttpRequest request) { return SendAsync(request.Cache.Data, request.Cache.Offset, request.Cache.Size); }
8888

8989
/// <summary>
9090
/// Send the HTTP request body (asynchronous)
@@ -257,7 +257,7 @@ public Task<HttpResponse> SendRequest(HttpRequest request, TimeSpan? timeout = n
257257

258258
void TimeoutHandler(object canceled)
259259
{
260-
if ((bool)canceled)
260+
if ((bool)canceled)
261261
return;
262262

263263
// Disconnect on timeout

source/NetCoreServer/HttpRequest.cs

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Tuple<string, string> Cookie(int i)
100100
/// <summary>
101101
/// Get the HTTP request cache content
102102
/// </summary>
103-
public string Cache { get { return _cache.ToString(); } }
103+
public Buffer Cache { get { return _cache; } }
104104

105105
/// <summary>
106106
/// Get string from the current HTTP request
@@ -283,7 +283,7 @@ public HttpRequest AddCookie(string name, string value)
283283
/// <summary>
284284
/// Set the HTTP request body
285285
/// </summary>
286-
/// <param name="body">Body content (default is "")</param>
286+
/// <param name="body">Body string content (default is "")</param>
287287
public HttpRequest SetBody(string body = "")
288288
{
289289
// Append content length header
@@ -302,6 +302,50 @@ public HttpRequest SetBody(string body = "")
302302
return this;
303303
}
304304

305+
/// <summary>
306+
/// Set the HTTP request body
307+
/// </summary>
308+
/// <param name="body">Body binary content</param>
309+
public HttpRequest SetBody(byte[] body)
310+
{
311+
// Append content length header
312+
SetHeader("Content-Length", body.Length.ToString());
313+
314+
_cache.Append("\r\n");
315+
316+
int index = (int)_cache.Size;
317+
318+
// Append the HTTP request body
319+
_cache.Append(body);
320+
_bodyIndex = index;
321+
_bodySize = body.Length;
322+
_bodyLength = body.Length;
323+
_bodyLengthProvided = true;
324+
return this;
325+
}
326+
327+
/// <summary>
328+
/// Set the HTTP request body
329+
/// </summary>
330+
/// <param name="body">Body buffer content</param>
331+
public HttpRequest SetBody(Buffer body)
332+
{
333+
// Append content length header
334+
SetHeader("Content-Length", body.Size.ToString());
335+
336+
_cache.Append("\r\n");
337+
338+
int index = (int)_cache.Size;
339+
340+
// Append the HTTP request body
341+
_cache.Append(body.Data, body.Offset, body.Size);
342+
_bodyIndex = index;
343+
_bodySize = (int)body.Size;
344+
_bodyLength = (int)body.Size;
345+
_bodyLengthProvided = true;
346+
return this;
347+
}
348+
305349
/// <summary>
306350
/// Set the HTTP request body length
307351
/// </summary>
@@ -351,7 +395,7 @@ public HttpRequest MakeGetRequest(string url)
351395
/// Make POST request
352396
/// </summary>
353397
/// <param name="url">URL to request</param>
354-
/// <param name="content">Content</param>
398+
/// <param name="content">String content</param>
355399
public HttpRequest MakePostRequest(string url, string content)
356400
{
357401
Clear();
@@ -360,11 +404,37 @@ public HttpRequest MakePostRequest(string url, string content)
360404
return this;
361405
}
362406

407+
/// <summary>
408+
/// Make POST request
409+
/// </summary>
410+
/// <param name="url">URL to request</param>
411+
/// <param name="content">Binary content</param>
412+
public HttpRequest MakePostRequest(string url, byte[] content)
413+
{
414+
Clear();
415+
SetBegin("POST", url);
416+
SetBody(content);
417+
return this;
418+
}
419+
420+
/// <summary>
421+
/// Make POST request
422+
/// </summary>
423+
/// <param name="url">URL to request</param>
424+
/// <param name="content">Buffer content</param>
425+
public HttpRequest MakePostRequest(string url, Buffer content)
426+
{
427+
Clear();
428+
SetBegin("POST", url);
429+
SetBody(content);
430+
return this;
431+
}
432+
363433
/// <summary>
364434
/// Make PUT request
365435
/// </summary>
366436
/// <param name="url">URL to request</param>
367-
/// <param name="content">Content</param>
437+
/// <param name="content">String content</param>
368438
public HttpRequest MakePutRequest(string url, string content)
369439
{
370440
Clear();
@@ -373,6 +443,32 @@ public HttpRequest MakePutRequest(string url, string content)
373443
return this;
374444
}
375445

446+
/// <summary>
447+
/// Make PUT request
448+
/// </summary>
449+
/// <param name="url">URL to request</param>
450+
/// <param name="content">Binary content</param>
451+
public HttpRequest MakePutRequest(string url, byte[] content)
452+
{
453+
Clear();
454+
SetBegin("PUT", url);
455+
SetBody(content);
456+
return this;
457+
}
458+
459+
/// <summary>
460+
/// Make PUT request
461+
/// </summary>
462+
/// <param name="url">URL to request</param>
463+
/// <param name="content">Buffer content</param>
464+
public HttpRequest MakePutRequest(string url, Buffer content)
465+
{
466+
Clear();
467+
SetBegin("PUT", url);
468+
SetBody(content);
469+
return this;
470+
}
471+
376472
/// <summary>
377473
/// Make DELETE request
378474
/// </summary>

source/NetCoreServer/HttpResponse.cs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Tuple<string, string> Header(int i)
8282
/// <summary>
8383
/// Get the HTTP response cache content
8484
/// </summary>
85-
public byte[] Cache { get { return _cache.Data; } }
85+
public Buffer Cache { get { return _cache; } }
8686

8787
/// <summary>
8888
/// Get string from the current HTTP response
@@ -468,8 +468,8 @@ public HttpResponse SetCookie(string name, string value, int maxAge = 86400, str
468468
/// <summary>
469469
/// Set the HTTP response body
470470
/// </summary>
471-
/// <param name="body">Body binary content</param>
472-
public HttpResponse SetBody(byte[] body)
471+
/// <param name="body">Body string content (default is "")</param>
472+
public HttpResponse SetBody(string body = "")
473473
{
474474
// Append content length header
475475
SetHeader("Content-Length", body.Length.ToString());
@@ -490,8 +490,8 @@ public HttpResponse SetBody(byte[] body)
490490
/// <summary>
491491
/// Set the HTTP response body
492492
/// </summary>
493-
/// <param name="body">Body content (default is "")</param>
494-
public HttpResponse SetBody(string body = "")
493+
/// <param name="body">Body binary content</param>
494+
public HttpResponse SetBody(byte[] body)
495495
{
496496
// Append content length header
497497
SetHeader("Content-Length", body.Length.ToString());
@@ -509,6 +509,28 @@ public HttpResponse SetBody(string body = "")
509509
return this;
510510
}
511511

512+
/// <summary>
513+
/// Set the HTTP response body
514+
/// </summary>
515+
/// <param name="body">Body buffer content</param>
516+
public HttpResponse SetBody(Buffer body)
517+
{
518+
// Append content length header
519+
SetHeader("Content-Length", body.Size.ToString());
520+
521+
_cache.Append("\r\n");
522+
523+
int index = (int)_cache.Size;
524+
525+
// Append the HTTP response body
526+
_cache.Append(body.Data, body.Offset, body.Size);
527+
_bodyIndex = index;
528+
_bodySize = (int)body.Size;
529+
_bodyLength = (int)body.Size;
530+
_bodyLengthProvided = true;
531+
return this;
532+
}
533+
512534
/// <summary>
513535
/// Set the HTTP response body length
514536
/// </summary>
@@ -572,7 +594,7 @@ public HttpResponse MakeHeadResponse()
572594
/// <summary>
573595
/// Make GET response
574596
/// </summary>
575-
/// <param name="body">Body content (default is "")</param>
597+
/// <param name="body">Body string content (default is "")</param>
576598
public HttpResponse MakeGetResponse(string body = "")
577599
{
578600
Clear();
@@ -582,6 +604,32 @@ public HttpResponse MakeGetResponse(string body = "")
582604
return this;
583605
}
584606

607+
/// <summary>
608+
/// Make GET response
609+
/// </summary>
610+
/// <param name="body">Body binary content</param>
611+
public HttpResponse MakeGetResponse(byte[] body)
612+
{
613+
Clear();
614+
SetBegin(200);
615+
SetHeader("Content-Type", "text/html; charset=UTF-8");
616+
SetBody(body);
617+
return this;
618+
}
619+
620+
/// <summary>
621+
/// Make GET response
622+
/// </summary>
623+
/// <param name="body">Body buffer content</param>
624+
public HttpResponse MakeGetResponse(Buffer body)
625+
{
626+
Clear();
627+
SetBegin(200);
628+
SetHeader("Content-Type", "text/html; charset=UTF-8");
629+
SetBody(body);
630+
return this;
631+
}
632+
585633
/// <summary>
586634
/// Make OPTIONS response
587635
/// </summary>
@@ -598,7 +646,7 @@ public HttpResponse MakeOptionsResponse(string allow = "HEAD,GET,POST,PUT,DELETE
598646
/// <summary>
599647
/// Make TRACE response
600648
/// </summary>
601-
/// <param name="request">Request content</param>
649+
/// <param name="request">Request string content</param>
602650
public HttpResponse MakeTraceResponse(string request)
603651
{
604652
Clear();
@@ -608,6 +656,32 @@ public HttpResponse MakeTraceResponse(string request)
608656
return this;
609657
}
610658

659+
/// <summary>
660+
/// Make TRACE response
661+
/// </summary>
662+
/// <param name="request">Request binary content</param>
663+
public HttpResponse MakeTraceResponse(byte[] request)
664+
{
665+
Clear();
666+
SetBegin(200);
667+
SetHeader("Content-Type", "message/http");
668+
SetBody(request);
669+
return this;
670+
}
671+
672+
/// <summary>
673+
/// Make TRACE response
674+
/// </summary>
675+
/// <param name="request">Request buffer content</param>
676+
public HttpResponse MakeTraceResponse(Buffer request)
677+
{
678+
Clear();
679+
SetBegin(200);
680+
SetHeader("Content-Type", "message/http");
681+
SetBody(request);
682+
return this;
683+
}
684+
611685
// HTTP response status phrase
612686
private int _statusPhraseIndex;
613687
private int _statusPhraseSize;

source/NetCoreServer/HttpServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool Handler(FileCache cache, string key, byte[] value, TimeSpan timespan)
5050
header.SetContentType(Path.GetExtension(key));
5151
header.SetHeader("Cache-Control", $"max-age={timespan.Seconds}");
5252
header.SetBody(value);
53-
return cache.Add(key, header.Cache, timespan);
53+
return cache.Add(key, header.Cache.Data, timespan);
5454
}
5555

5656
Cache.InsertPath(path, prefix, timeout.Value, Handler);

source/NetCoreServer/HttpSession.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public HttpSession(HttpServer server) : base(server)
4040
/// </summary>
4141
/// <param name="response">HTTP response</param>
4242
/// <returns>Size of sent data</returns>
43-
public long SendResponse(HttpResponse response) { return Send(response.Cache); }
43+
public long SendResponse(HttpResponse response) { return Send(response.Cache.Data, response.Cache.Offset, response.Cache.Size); }
4444

4545
/// <summary>
4646
/// Send the HTTP response body (synchronous)
@@ -73,7 +73,7 @@ public HttpSession(HttpServer server) : base(server)
7373
/// </summary>
7474
/// <param name="response">HTTP response</param>
7575
/// <returns>'true' if the current HTTP response was successfully sent, 'false' if the session is not connected</returns>
76-
public bool SendResponseAsync(HttpResponse response) { return SendAsync(response.Cache); }
76+
public bool SendResponseAsync(HttpResponse response) { return SendAsync(response.Cache.Data, response.Cache.Offset, response.Cache.Size); }
7777

7878
/// <summary>
7979
/// Send the HTTP response body (asynchronous)
@@ -100,7 +100,7 @@ public HttpSession(HttpServer server) : base(server)
100100

101101
#region Session handlers
102102

103-
protected override void OnReceived(byte[] buffer, long offset, long size)
103+
protected override void OnReceived(byte[] buffer, long offset, long size)
104104
{
105105
// Receive HTTP request header
106106
if (Request.IsPendingHeader())

source/NetCoreServer/HttpsClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class HttpsClient : SslClient
5454
/// </summary>
5555
/// <param name="request">HTTP request</param>
5656
/// <returns>Size of sent data</returns>
57-
public long SendRequest(HttpRequest request) { return Send(request.Cache); }
57+
public long SendRequest(HttpRequest request) { return Send(request.Cache.Data, request.Cache.Offset, request.Cache.Size); }
5858

5959
/// <summary>
6060
/// Send the HTTP request body (synchronous)
@@ -87,7 +87,7 @@ public class HttpsClient : SslClient
8787
/// </summary>
8888
/// <param name="request">HTTP request</param>
8989
/// <returns>'true' if the current HTTP request was successfully sent, 'false' if the session is not connected</returns>
90-
public bool SendRequestAsync(HttpRequest request) { return SendAsync(request.Cache); }
90+
public bool SendRequestAsync(HttpRequest request) { return SendAsync(request.Cache.Data, request.Cache.Offset, request.Cache.Size); }
9191

9292
/// <summary>
9393
/// Send the HTTP request body (asynchronous)
@@ -264,7 +264,7 @@ public Task<HttpResponse> SendRequest(HttpRequest request, TimeSpan? timeout = n
264264

265265
void TimeoutHandler(object canceled)
266266
{
267-
if ((bool)canceled)
267+
if ((bool)canceled)
268268
return;
269269

270270
// Disconnect on timeout

0 commit comments

Comments
 (0)