HttpRequest.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. namespace NetCoreServer
  6. {
  7. /// <summary>
  8. /// HTTP request is used to create or process parameters of HTTP protocol request(method, URL, headers, etc).
  9. /// </summary>
  10. /// <remarks>Not thread-safe.</remarks>
  11. public class HttpRequest
  12. {
  13. /// <summary>
  14. /// Initialize an empty HTTP request
  15. /// </summary>
  16. public HttpRequest()
  17. {
  18. Clear();
  19. }
  20. /// <summary>
  21. /// Initialize a new HTTP request with a given method, URL and protocol
  22. /// </summary>
  23. /// <param name="method">HTTP method</param>
  24. /// <param name="url">Requested URL</param>
  25. /// <param name="protocol">Protocol version (default is "HTTP/1.1")</param>
  26. public HttpRequest(string method, string url, string protocol = "HTTP/1.1")
  27. {
  28. SetBegin(method, url, protocol);
  29. }
  30. /// <summary>
  31. /// Is the HTTP request empty?
  32. /// </summary>
  33. public bool IsEmpty { get { return (_cache.Size == 0); } }
  34. /// <summary>
  35. /// Is the HTTP request error flag set?
  36. /// </summary>
  37. public bool IsErrorSet { get; private set; }
  38. /// <summary>
  39. /// Get the HTTP request method
  40. /// </summary>
  41. public string Method { get { return _method; } }
  42. /// <summary>
  43. /// Get the HTTP request URL
  44. /// </summary>
  45. public string Url { get { return _url; } }
  46. /// <summary>
  47. /// Get the HTTP request protocol version
  48. /// </summary>
  49. public string Protocol { get { return _protocol; } }
  50. /// <summary>
  51. /// Get the HTTP request headers count
  52. /// </summary>
  53. public long Headers { get { return _headers.Count; } }
  54. /// <summary>
  55. /// Get the HTTP request header by index
  56. /// </summary>
  57. public (string, string) Header(int i)
  58. {
  59. Debug.Assert((i < _headers.Count), "Index out of bounds!");
  60. if (i >= _headers.Count)
  61. return ("", "");
  62. return _headers[i];
  63. }
  64. /// <summary>
  65. /// Get the HTTP request cookies count
  66. /// </summary>
  67. public long Cookies { get { return _cookies.Count; } }
  68. /// <summary>
  69. /// Get the HTTP request cookie by index
  70. /// </summary>
  71. public (string, string) Cookie(int i)
  72. {
  73. Debug.Assert((i < _cookies.Count), "Index out of bounds!");
  74. if (i >= _cookies.Count)
  75. return ("", "");
  76. return _cookies[i];
  77. }
  78. /// <summary>
  79. /// Get the HTTP request body as string
  80. /// </summary>
  81. public string Body { get { return _cache.ExtractString(_bodyIndex, _bodySize); } }
  82. /// <summary>
  83. /// Get the HTTP request body as byte array
  84. /// </summary>
  85. public byte[] BodyBytes { get { return _cache.Data[_bodyIndex..(_bodyIndex + _bodySize)]; } }
  86. /// <summary>
  87. /// Get the HTTP request body as byte span
  88. /// </summary>
  89. public Span<byte> BodySpan { get { return new Span<byte>(_cache.Data, _bodyIndex, _bodySize); } }
  90. /// <summary>
  91. /// Get the HTTP request body length
  92. /// </summary>
  93. public long BodyLength { get { return _bodyLength; } }
  94. /// <summary>
  95. /// Get the HTTP request cache content
  96. /// </summary>
  97. public Buffer Cache { get { return _cache; } }
  98. /// <summary>
  99. /// Get string from the current HTTP request
  100. /// </summary>
  101. public override string ToString()
  102. {
  103. StringBuilder sb = new StringBuilder();
  104. sb.AppendLine($"Request method: {Method}");
  105. sb.AppendLine($"Request URL: {Url}");
  106. sb.AppendLine($"Request protocol: {Protocol}");
  107. sb.AppendLine($"Request headers: {Headers}");
  108. for (int i = 0; i < Headers; i++)
  109. {
  110. var header = Header(i);
  111. sb.AppendLine($"{header.Item1} : {header.Item2}");
  112. }
  113. sb.AppendLine($"Request body: {BodyLength}");
  114. sb.AppendLine(Body);
  115. return sb.ToString();
  116. }
  117. /// <summary>
  118. /// Clear the HTTP request cache
  119. /// </summary>
  120. public HttpRequest Clear()
  121. {
  122. IsErrorSet = false;
  123. _method = "";
  124. _url = "";
  125. _protocol = "";
  126. _headers.Clear();
  127. _cookies.Clear();
  128. _bodyIndex = 0;
  129. _bodySize = 0;
  130. _bodyLength = 0;
  131. _bodyLengthProvided = false;
  132. _cache.Clear();
  133. _cacheSize = 0;
  134. return this;
  135. }
  136. /// <summary>
  137. /// Set the HTTP request begin with a given method, URL and protocol
  138. /// </summary>
  139. /// <param name="method">HTTP method</param>
  140. /// <param name="url">Requested URL</param>
  141. /// <param name="protocol">Protocol version (default is "HTTP/1.1")</param>
  142. public HttpRequest SetBegin(string method, string url, string protocol = "HTTP/1.1")
  143. {
  144. // Clear the HTTP request cache
  145. Clear();
  146. // Append the HTTP request method
  147. _cache.Append(method);
  148. _method = method;
  149. _cache.Append(" ");
  150. // Append the HTTP request URL
  151. _cache.Append(url);
  152. _url = url;
  153. _cache.Append(" ");
  154. // Append the HTTP request protocol version
  155. _cache.Append(protocol);
  156. _protocol = protocol;
  157. _cache.Append("\r\n");
  158. return this;
  159. }
  160. /// <summary>
  161. /// Set the HTTP request header
  162. /// </summary>
  163. /// <param name="key">Header key</param>
  164. /// <param name="value">Header value</param>
  165. public HttpRequest SetHeader(string key, string value)
  166. {
  167. // Append the HTTP request header's key
  168. _cache.Append(key);
  169. _cache.Append(": ");
  170. // Append the HTTP request header's value
  171. _cache.Append(value);
  172. _cache.Append("\r\n");
  173. // Add the header to the corresponding collection
  174. _headers.Add((key, value));
  175. return this;
  176. }
  177. /// <summary>
  178. /// Set the HTTP request cookie
  179. /// </summary>
  180. /// <param name="name">Cookie name</param>
  181. /// <param name="value">Cookie value</param>
  182. public HttpRequest SetCookie(string name, string value)
  183. {
  184. string key = "Cookie";
  185. string cookie = name + "=" + value;
  186. // Append the HTTP request header's key
  187. _cache.Append(key);
  188. _cache.Append(": ");
  189. // Append Cookie
  190. _cache.Append(cookie);
  191. _cache.Append("\r\n");
  192. // Add the header to the corresponding collection
  193. _headers.Add((key, cookie));
  194. // Add the cookie to the corresponding collection
  195. _cookies.Add((name, value));
  196. return this;
  197. }
  198. /// <summary>
  199. /// Add the HTTP request cookie
  200. /// </summary>
  201. /// <param name="name">Cookie name</param>
  202. /// <param name="value">Cookie value</param>
  203. public HttpRequest AddCookie(string name, string value)
  204. {
  205. // Append Cookie
  206. _cache.Append("; ");
  207. _cache.Append(name);
  208. _cache.Append("=");
  209. _cache.Append(value);
  210. // Add the cookie to the corresponding collection
  211. _cookies.Add((name, value));
  212. return this;
  213. }
  214. /// <summary>
  215. /// Set the HTTP request body
  216. /// </summary>
  217. /// <param name="body">Body string content (default is "")</param>
  218. public HttpRequest SetBody(string body = "") => SetBody(body.AsSpan());
  219. /// <summary>
  220. /// Set the HTTP request body
  221. /// </summary>
  222. /// <param name="body">Body string content as a span of characters</param>
  223. public HttpRequest SetBody(ReadOnlySpan<char> body)
  224. {
  225. int length = body.IsEmpty ? 0 : Encoding.UTF8.GetByteCount(body);
  226. // Append content length header
  227. SetHeader("Content-Length", length.ToString());
  228. _cache.Append("\r\n");
  229. int index = (int)_cache.Size;
  230. // Append the HTTP request body
  231. _cache.Append(body);
  232. _bodyIndex = index;
  233. _bodySize = length;
  234. _bodyLength = length;
  235. _bodyLengthProvided = true;
  236. return this;
  237. }
  238. /// <summary>
  239. /// Set the HTTP request body
  240. /// </summary>
  241. /// <param name="body">Body binary content</param>
  242. public HttpRequest SetBody(byte[] body) => SetBody(body.AsSpan());
  243. /// <summary>
  244. /// Set the HTTP request body
  245. /// </summary>
  246. /// <param name="body">Body binary content as a span of bytes</param>
  247. public HttpRequest SetBody(ReadOnlySpan<byte> body)
  248. {
  249. // Append content length header
  250. SetHeader("Content-Length", body.Length.ToString());
  251. _cache.Append("\r\n");
  252. int index = (int)_cache.Size;
  253. // Append the HTTP request body
  254. _cache.Append(body);
  255. _bodyIndex = index;
  256. _bodySize = body.Length;
  257. _bodyLength = body.Length;
  258. _bodyLengthProvided = true;
  259. return this;
  260. }
  261. /// <summary>
  262. /// Set the HTTP request body length
  263. /// </summary>
  264. /// <param name="length">Body length</param>
  265. public HttpRequest SetBodyLength(int length)
  266. {
  267. // Append content length header
  268. SetHeader("Content-Length", length.ToString());
  269. _cache.Append("\r\n");
  270. int index = (int)_cache.Size;
  271. // Clear the HTTP request body
  272. _bodyIndex = index;
  273. _bodySize = 0;
  274. _bodyLength = length;
  275. _bodyLengthProvided = true;
  276. return this;
  277. }
  278. /// <summary>
  279. /// Make HEAD request
  280. /// </summary>
  281. /// <param name="url">URL to request</param>
  282. public HttpRequest MakeHeadRequest(string url)
  283. {
  284. Clear();
  285. SetBegin("HEAD", url);
  286. SetBody();
  287. return this;
  288. }
  289. /// <summary>
  290. /// Make GET request
  291. /// </summary>
  292. /// <param name="url">URL to request</param>
  293. public HttpRequest MakeGetRequest(string url)
  294. {
  295. Clear();
  296. SetBegin("GET", url);
  297. SetBody();
  298. return this;
  299. }
  300. /// <summary>
  301. /// Make POST request
  302. /// </summary>
  303. /// <param name="url">URL to request</param>
  304. /// <param name="content">String content</param>
  305. /// <param name="contentType">Content type (default is "text/plain; charset=UTF-8")</param>
  306. public HttpRequest MakePostRequest(string url, string content, string contentType = "text/plain; charset=UTF-8") => MakePostRequest(url, content.AsSpan(), contentType);
  307. /// <summary>
  308. /// Make POST request
  309. /// </summary>
  310. /// <param name="url">URL to request</param>
  311. /// <param name="content">String content as a span of characters</param>
  312. /// <param name="contentType">Content type (default is "text/plain; charset=UTF-8")</param>
  313. public HttpRequest MakePostRequest(string url, ReadOnlySpan<char> content, string contentType = "text/plain; charset=UTF-8")
  314. {
  315. Clear();
  316. SetBegin("POST", url);
  317. if (!string.IsNullOrEmpty(contentType))
  318. SetHeader("Content-Type", contentType);
  319. SetBody(content);
  320. return this;
  321. }
  322. /// <summary>
  323. /// Make POST request
  324. /// </summary>
  325. /// <param name="url">URL to request</param>
  326. /// <param name="content">Binary content</param>
  327. /// <param name="contentType">Content type (default is "")</param>
  328. public HttpRequest MakePostRequest(string url, byte[] content, string contentType = "") => MakePostRequest(url, content.AsSpan(), contentType);
  329. /// <summary>
  330. /// Make POST request
  331. /// </summary>
  332. /// <param name="url">URL to request</param>
  333. /// <param name="content">Binary content as a span of bytes</param>
  334. /// <param name="contentType">Content type (default is "")</param>
  335. public HttpRequest MakePostRequest(string url, ReadOnlySpan<byte> content, string contentType = "")
  336. {
  337. Clear();
  338. SetBegin("POST", url);
  339. if (!string.IsNullOrEmpty(contentType))
  340. SetHeader("Content-Type", contentType);
  341. SetBody(content);
  342. return this;
  343. }
  344. /// <summary>
  345. /// Make PUT request
  346. /// </summary>
  347. /// <param name="url">URL to request</param>
  348. /// <param name="content">String content</param>
  349. /// <param name="contentType">Content type (default is "text/plain; charset=UTF-8")</param>
  350. public HttpRequest MakePutRequest(string url, string content, string contentType = "text/plain; charset=UTF-8") => MakePutRequest(url, content.AsSpan(), contentType);
  351. /// <summary>
  352. /// Make PUT request
  353. /// </summary>
  354. /// <param name="url">URL to request</param>
  355. /// <param name="content">String content as a span of characters</param>
  356. /// <param name="contentType">Content type (default is "text/plain; charset=UTF-8")</param>
  357. public HttpRequest MakePutRequest(string url, ReadOnlySpan<char> content, string contentType = "text/plain; charset=UTF-8")
  358. {
  359. Clear();
  360. SetBegin("PUT", url);
  361. if (!string.IsNullOrEmpty(contentType))
  362. SetHeader("Content-Type", contentType);
  363. SetBody(content);
  364. return this;
  365. }
  366. /// <summary>
  367. /// Make PUT request
  368. /// </summary>
  369. /// <param name="url">URL to request</param>
  370. /// <param name="content">Binary content</param>
  371. /// <param name="contentType">Content type (default is "")</param>
  372. public HttpRequest MakePutRequest(string url, byte[] content, string contentType = "") => MakePutRequest(url, content.AsSpan(), contentType);
  373. /// <summary>
  374. /// Make PUT request
  375. /// </summary>
  376. /// <param name="url">URL to request</param>
  377. /// <param name="content">Binary content as a span of bytes</param>
  378. /// <param name="contentType">Content type (default is "")</param>
  379. public HttpRequest MakePutRequest(string url, ReadOnlySpan<byte> content, string contentType = "")
  380. {
  381. Clear();
  382. SetBegin("PUT", url);
  383. if (!string.IsNullOrEmpty(contentType))
  384. SetHeader("Content-Type", contentType);
  385. SetBody(content);
  386. return this;
  387. }
  388. /// <summary>
  389. /// Make DELETE request
  390. /// </summary>
  391. /// <param name="url">URL to request</param>
  392. public HttpRequest MakeDeleteRequest(string url)
  393. {
  394. Clear();
  395. SetBegin("DELETE", url);
  396. SetBody();
  397. return this;
  398. }
  399. /// <summary>
  400. /// Make OPTIONS request
  401. /// </summary>
  402. /// <param name="url">URL to request</param>
  403. public HttpRequest MakeOptionsRequest(string url)
  404. {
  405. Clear();
  406. SetBegin("OPTIONS", url);
  407. SetBody();
  408. return this;
  409. }
  410. /// <summary>
  411. /// Make TRACE request
  412. /// </summary>
  413. /// <param name="url">URL to request</param>
  414. public HttpRequest MakeTraceRequest(string url)
  415. {
  416. Clear();
  417. SetBegin("TRACE", url);
  418. SetBody();
  419. return this;
  420. }
  421. // HTTP request method
  422. private string _method;
  423. // HTTP request URL
  424. private string _url;
  425. // HTTP request protocol
  426. private string _protocol;
  427. // HTTP request headers
  428. private List<(string, string)> _headers = new List<(string, string)>();
  429. // HTTP request cookies
  430. private List<(string, string)> _cookies = new List<(string, string)>();
  431. // HTTP request body
  432. private int _bodyIndex;
  433. private int _bodySize;
  434. private int _bodyLength;
  435. private bool _bodyLengthProvided;
  436. // HTTP request cache
  437. private Buffer _cache = new Buffer();
  438. private int _cacheSize;
  439. // Is pending parts of HTTP request
  440. internal bool IsPendingHeader()
  441. {
  442. return (!IsErrorSet && (_bodyIndex == 0));
  443. }
  444. internal bool IsPendingBody()
  445. {
  446. return (!IsErrorSet && (_bodyIndex > 0) && (_bodySize > 0));
  447. }
  448. internal bool ReceiveHeader(byte[] buffer, int offset, int size)
  449. {
  450. // Update the request cache
  451. _cache.Append(buffer, offset, size);
  452. // Try to seek for HTTP header separator
  453. for (int i = _cacheSize; i < (int)_cache.Size; i++)
  454. {
  455. // Check for the request cache out of bounds
  456. if ((i + 3) >= (int)_cache.Size)
  457. break;
  458. // Check for the header separator
  459. if ((_cache[i + 0] == '\r') && (_cache[i + 1] == '\n') && (_cache[i + 2] == '\r') && (_cache[i + 3] == '\n'))
  460. {
  461. int index = 0;
  462. // Set the error flag for a while...
  463. IsErrorSet = true;
  464. // Parse method
  465. int methodIndex = index;
  466. int methodSize = 0;
  467. while (_cache[index] != ' ')
  468. {
  469. methodSize++;
  470. index++;
  471. if (index >= (int)_cache.Size)
  472. return false;
  473. }
  474. index++;
  475. if (index >= (int)_cache.Size)
  476. return false;
  477. _method = _cache.ExtractString(methodIndex, methodSize);
  478. // Parse URL
  479. int urlIndex = index;
  480. int urlSize = 0;
  481. while (_cache[index] != ' ')
  482. {
  483. urlSize++;
  484. index++;
  485. if (index >= (int)_cache.Size)
  486. return false;
  487. }
  488. index++;
  489. if (index >= (int)_cache.Size)
  490. return false;
  491. _url = _cache.ExtractString(urlIndex, urlSize);
  492. // Parse protocol version
  493. int protocolIndex = index;
  494. int protocolSize = 0;
  495. while (_cache[index] != '\r')
  496. {
  497. protocolSize++;
  498. index++;
  499. if (index >= (int)_cache.Size)
  500. return false;
  501. }
  502. index++;
  503. if ((index >= (int)_cache.Size) || (_cache[index] != '\n'))
  504. return false;
  505. index++;
  506. if (index >= (int)_cache.Size)
  507. return false;
  508. _protocol = _cache.ExtractString(protocolIndex, protocolSize);
  509. // Parse headers
  510. while ((index < (int)_cache.Size) && (index < i))
  511. {
  512. // Parse header name
  513. int headerNameIndex = index;
  514. int headerNameSize = 0;
  515. while (_cache[index] != ':')
  516. {
  517. headerNameSize++;
  518. index++;
  519. if (index >= i)
  520. break;
  521. if (index >= (int)_cache.Size)
  522. return false;
  523. }
  524. index++;
  525. if (index >= i)
  526. break;
  527. if (index >= (int)_cache.Size)
  528. return false;
  529. // Skip all prefix space characters
  530. while (char.IsWhiteSpace((char)_cache[index]))
  531. {
  532. index++;
  533. if (index >= i)
  534. break;
  535. if (index >= (int)_cache.Size)
  536. return false;
  537. }
  538. // Parse header value
  539. int headerValueIndex = index;
  540. int headerValueSize = 0;
  541. while (_cache[index] != '\r')
  542. {
  543. headerValueSize++;
  544. index++;
  545. if (index >= i)
  546. break;
  547. if (index >= (int)_cache.Size)
  548. return false;
  549. }
  550. index++;
  551. if ((index >= (int)_cache.Size) || (_cache[index] != '\n'))
  552. return false;
  553. index++;
  554. if (index >= (int)_cache.Size)
  555. return false;
  556. // Validate header name and value (sometimes value can be empty)
  557. if (headerNameSize == 0)
  558. return false;
  559. // Add a new header
  560. string headerName = _cache.ExtractString(headerNameIndex, headerNameSize);
  561. string headerValue = _cache.ExtractString(headerValueIndex, headerValueSize);
  562. _headers.Add((headerName, headerValue));
  563. // Try to find the body content length
  564. if (string.Compare(headerName, "Content-Length", StringComparison.OrdinalIgnoreCase) == 0)
  565. {
  566. _bodyLength = 0;
  567. for (int j = headerValueIndex; j < (headerValueIndex + headerValueSize); j++)
  568. {
  569. if ((_cache[j] < '0') || (_cache[j] > '9'))
  570. return false;
  571. _bodyLength *= 10;
  572. _bodyLength += _cache[j] - '0';
  573. _bodyLengthProvided = true;
  574. }
  575. }
  576. // Try to find Cookies
  577. if (string.Compare(headerName, "Cookie", StringComparison.OrdinalIgnoreCase) == 0)
  578. {
  579. bool name = true;
  580. bool token = false;
  581. int current = headerValueIndex;
  582. int nameIndex = index;
  583. int nameSize = 0;
  584. int cookieIndex = index;
  585. int cookieSize = 0;
  586. for (int j = headerValueIndex; j < (headerValueIndex + headerValueSize); j++)
  587. {
  588. if (_cache[j] == ' ')
  589. {
  590. if (token)
  591. {
  592. if (name)
  593. {
  594. nameIndex = current;
  595. nameSize = j - current;
  596. }
  597. else
  598. {
  599. cookieIndex = current;
  600. cookieSize = j - current;
  601. }
  602. }
  603. token = false;
  604. continue;
  605. }
  606. if (_cache[j] == '=')
  607. {
  608. if (token)
  609. {
  610. if (name)
  611. {
  612. nameIndex = current;
  613. nameSize = j - current;
  614. }
  615. else
  616. {
  617. cookieIndex = current;
  618. cookieSize = j - current;
  619. }
  620. }
  621. token = false;
  622. name = false;
  623. continue;
  624. }
  625. if (_cache[j] == ';')
  626. {
  627. if (token)
  628. {
  629. if (name)
  630. {
  631. nameIndex = current;
  632. nameSize = j - current;
  633. }
  634. else
  635. {
  636. cookieIndex = current;
  637. cookieSize = j - current;
  638. }
  639. // Validate the cookie
  640. if ((nameSize > 0) && (cookieSize > 0))
  641. {
  642. // Add the cookie to the corresponding collection
  643. _cookies.Add((_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize)));
  644. // Resset the current cookie values
  645. nameIndex = j;
  646. nameSize = 0;
  647. cookieIndex = j;
  648. cookieSize = 0;
  649. }
  650. }
  651. token = false;
  652. name = true;
  653. continue;
  654. }
  655. if (!token)
  656. {
  657. current = j;
  658. token = true;
  659. }
  660. }
  661. // Process the last cookie
  662. if (token)
  663. {
  664. if (name)
  665. {
  666. nameIndex = current;
  667. nameSize = headerValueIndex + headerValueSize - current;
  668. }
  669. else
  670. {
  671. cookieIndex = current;
  672. cookieSize = headerValueIndex + headerValueSize - current;
  673. }
  674. // Validate the cookie
  675. if ((nameSize > 0) && (cookieSize > 0))
  676. {
  677. // Add the cookie to the corresponding collection
  678. _cookies.Add((_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize)));
  679. }
  680. }
  681. }
  682. }
  683. // Reset the error flag
  684. IsErrorSet = false;
  685. // Update the body index and size
  686. _bodyIndex = i + 4;
  687. _bodySize = (int)_cache.Size - i - 4;
  688. // Update the parsed cache size
  689. _cacheSize = (int)_cache.Size;
  690. return true;
  691. }
  692. }
  693. // Update the parsed cache size
  694. _cacheSize = ((int)_cache.Size >= 3) ? ((int)_cache.Size - 3) : 0;
  695. return false;
  696. }
  697. internal bool ReceiveBody(byte[] buffer, int offset, int size)
  698. {
  699. // Update the request cache
  700. _cache.Append(buffer, offset, size);
  701. // Update the parsed cache size
  702. _cacheSize = (int)_cache.Size;
  703. // Update body size
  704. _bodySize += size;
  705. // Check if the body length was provided
  706. if (_bodyLengthProvided)
  707. {
  708. // Was the body fully received?
  709. if (_bodySize >= _bodyLength)
  710. {
  711. _bodySize = _bodyLength;
  712. return true;
  713. }
  714. }
  715. else
  716. {
  717. // HEAD/GET/DELETE/OPTIONS/TRACE request might have no body
  718. if ((Method == "HEAD") || (Method == "GET") || (Method == "DELETE") || (Method == "OPTIONS") || (Method == "TRACE"))
  719. {
  720. _bodyLength = 0;
  721. _bodySize = 0;
  722. return true;
  723. }
  724. // Check the body content to find the request body end
  725. if (_bodySize >= 4)
  726. {
  727. int index = _bodyIndex + _bodySize - 4;
  728. // Was the body fully received?
  729. if ((_cache[index + 0] == '\r') && (_cache[index + 1] == '\n') && (_cache[index + 2] == '\r') && (_cache[index + 3] == '\n'))
  730. {
  731. _bodyLength = _bodySize;
  732. return true;
  733. }
  734. }
  735. }
  736. // Body was received partially...
  737. return false;
  738. }
  739. }
  740. }