HttpServer.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. //==========================================================================
  2. // File: HttpServer.cs
  3. //
  4. // Summary: Implements an HttpServer to be used by the HttpServerChannel class
  5. //
  6. //
  7. // Classes: internal sealed HttpServer
  8. // internal sealed ReqMessageParser
  9. // private RequestArguments
  10. //
  11. // By :
  12. // Ahmad Tantawy [email protected]
  13. // Ahmad Kadry [email protected]
  14. // Hussein Mehanna [email protected]
  15. //
  16. //==========================================================================
  17. //
  18. // Permission is hereby granted, free of charge, to any person obtaining
  19. // a copy of this software and associated documentation files (the
  20. // "Software"), to deal in the Software without restriction, including
  21. // without limitation the rights to use, copy, modify, merge, publish,
  22. // distribute, sublicense, and/or sell copies of the Software, and to
  23. // permit persons to whom the Software is furnished to do so, subject to
  24. // the following conditions:
  25. //
  26. // The above copyright notice and this permission notice shall be
  27. // included in all copies or substantial portions of the Software.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  33. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. //
  37. using System;
  38. using System.Net.Sockets;
  39. using System.Text;
  40. using System.Text.RegularExpressions;
  41. using System.Collections;
  42. using System.Runtime.Remoting.Channels;
  43. using System.IO;
  44. using System.Net;
  45. using System.Runtime.Remoting.Messaging;
  46. namespace System.Runtime.Remoting.Channels.Http
  47. {
  48. internal class RequestArguments
  49. {
  50. static int count;
  51. Socket socket;
  52. public RequestArguments (Socket socket, HttpServerTransportSink sink)
  53. {
  54. Id = count++;
  55. NetworkStream ns = new NetworkStream (socket);
  56. this.Stream = ns;
  57. this.socket = socket;
  58. Sink = sink;
  59. }
  60. public void Process ()
  61. {
  62. HttpServer.ProcessRequest (this);
  63. }
  64. public IPAddress ClientAddress
  65. {
  66. get {
  67. IPEndPoint ep = socket.RemoteEndPoint as IPEndPoint;
  68. if (ep != null) return ep.Address;
  69. else return null;
  70. }
  71. }
  72. public int Id;
  73. public Stream Stream;
  74. public HttpServerTransportSink Sink;
  75. }
  76. internal sealed class HttpServer
  77. {
  78. public static void ProcessRequest (RequestArguments reqArg)
  79. {
  80. //Step (1) Start Reciceve the header
  81. ArrayList Headers = RecieveHeader (reqArg);
  82. //Step (2) Start Parse the header
  83. IDictionary HeaderFields = new Hashtable();
  84. IDictionary CustomHeaders = new Hashtable();
  85. if (!ParseHeader (reqArg, Headers, HeaderFields, CustomHeaders))
  86. return;
  87. //Step (3)
  88. if (!CheckRequest (reqArg, HeaderFields, CustomHeaders))
  89. return;
  90. //Step (4) Recieve the entity body
  91. byte[] buffer;
  92. object len = HeaderFields["content-length"];
  93. if (len != null)
  94. {
  95. buffer = new byte [(int)len];
  96. if (!RecieveEntityBody (reqArg, buffer))
  97. return;
  98. }
  99. else
  100. buffer = new byte [0];
  101. //Step (5)
  102. SendRequestForChannel (reqArg, HeaderFields, CustomHeaders, buffer);
  103. }
  104. private static ArrayList RecieveHeader (RequestArguments reqArg)
  105. {
  106. bool bLastLine = false;
  107. bool bEndOfLine = false;
  108. byte[] buffer = new byte[1024];
  109. ArrayList Headers = new ArrayList();
  110. Stream ist = reqArg.Stream;
  111. int index =0;
  112. while (!bLastLine)
  113. {
  114. //recieve line by line
  115. index = 0;
  116. bEndOfLine = false;
  117. //Step (1) is it an empty line?
  118. ist.Read (buffer, index, 1);
  119. if(buffer[index++]==13)
  120. {
  121. ist.Read (buffer, index, 1);
  122. bLastLine=true;
  123. bEndOfLine = true;
  124. }
  125. //Step (2) recieve line bytes
  126. while (!bEndOfLine)
  127. {
  128. ist.Read (buffer, index, 1);
  129. if(buffer [index++]==13)
  130. {
  131. bEndOfLine = true;
  132. ist.Read (buffer,index,1);
  133. }
  134. }
  135. //Step (3) convert bytes to a string
  136. if (bLastLine)
  137. continue;
  138. Headers.Add (Encoding.ASCII.GetString (buffer,0,index));
  139. }//end while loop
  140. return Headers;
  141. }
  142. private static bool ParseHeader (RequestArguments reqArg, ArrayList Headers, IDictionary HeaderFields, IDictionary CustomHeaders)
  143. {
  144. for (int i=0;i<Headers.Count;i++)
  145. {
  146. if (ReqMessageParser.ParseHeaderField ((string)Headers[i],HeaderFields))
  147. continue;
  148. if (!ReqMessageParser.IsCustomHeader((string)Headers[i],CustomHeaders ) )
  149. {
  150. SendResponse (reqArg, 400, null, null);
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. private static bool CheckRequest (RequestArguments reqArg, IDictionary HeaderFields, IDictionary CustomHeaders)
  157. {
  158. string temp;
  159. if (HeaderFields["expect"] as string == "100-continue")
  160. SendResponse (reqArg, 100, null, null);
  161. //Check the method
  162. temp = HeaderFields["method"].ToString();
  163. if (temp != "POST")
  164. return true;
  165. //Check for the content-length field
  166. if (HeaderFields["content-length"]==null)
  167. {
  168. SendResponse (reqArg, 411, null, null);
  169. return false;
  170. }
  171. return true;
  172. }
  173. private static bool RecieveEntityBody (RequestArguments reqArg, byte[] buffer)
  174. {
  175. try
  176. {
  177. int nr = 0;
  178. while (nr < buffer.Length)
  179. nr += reqArg.Stream.Read (buffer, nr, buffer.Length - nr);
  180. }
  181. catch (SocketException e)
  182. {
  183. switch(e.ErrorCode)
  184. {
  185. case 10060 : //TimeOut
  186. SendResponse (reqArg, 408, null, null);
  187. break;
  188. default :
  189. throw e;
  190. }
  191. return false;
  192. }//end catch
  193. return true;
  194. }
  195. private static bool SendRequestForChannel (RequestArguments reqArg, IDictionary HeaderFields, IDictionary CustomHeaders, byte[] buffer)
  196. {
  197. TransportHeaders THeaders = new TransportHeaders();
  198. Stream stream = new MemoryStream(buffer);
  199. if(stream.Position !=0)
  200. stream.Seek(0,SeekOrigin.Begin);
  201. THeaders[CommonTransportKeys.RequestUri] = FixURI((string)HeaderFields["request-url"]);
  202. THeaders[CommonTransportKeys.ContentType]= HeaderFields["content-type"];
  203. THeaders[CommonTransportKeys.RequestVerb]= HeaderFields["method"];
  204. THeaders[CommonTransportKeys.HttpVersion] = HeaderFields["http-version"];
  205. THeaders[CommonTransportKeys.UserAgent] = HeaderFields["user-agent"];
  206. THeaders[CommonTransportKeys.Host] = HeaderFields["host"];
  207. THeaders[CommonTransportKeys.SoapAction] = HeaderFields["SOAPAction"];
  208. THeaders[CommonTransportKeys.IPAddress] = reqArg.ClientAddress;
  209. THeaders[CommonTransportKeys.ConnectionId] = reqArg.Id;
  210. foreach(DictionaryEntry DictEntry in CustomHeaders)
  211. {
  212. THeaders[DictEntry.Key.ToString()] = DictEntry.Value.ToString();
  213. }
  214. reqArg.Sink.ServiceRequest (reqArg, stream, THeaders);
  215. return true;
  216. }
  217. private static string FixURI(string RequestURI)
  218. {
  219. if(RequestURI.IndexOf ( '.' ) == -1)
  220. return RequestURI;
  221. else
  222. return RequestURI.Substring(1);
  223. }
  224. public static void SendResponse (RequestArguments reqArg, int httpStatusCode, ITransportHeaders headers, Stream responseStream)
  225. {
  226. byte [] headersBuffer = null;
  227. byte [] entityBuffer = null;
  228. StringBuilder responseStr;
  229. String reason = null;
  230. if (headers != null && headers[CommonTransportKeys.HttpStatusCode] != null) {
  231. // The formatter can override the result code
  232. httpStatusCode = int.Parse ((string)headers [CommonTransportKeys.HttpStatusCode]);
  233. reason = (string) headers [CommonTransportKeys.HttpReasonPhrase];
  234. }
  235. if (reason == null)
  236. reason = GetReasonPhrase (httpStatusCode);
  237. //Response Line
  238. responseStr = new StringBuilder ("HTTP/1.0 " + httpStatusCode + " " + reason + "\r\n" );
  239. if (headers != null)
  240. {
  241. foreach (DictionaryEntry entry in headers)
  242. {
  243. string key = entry.Key.ToString();
  244. if (key != CommonTransportKeys.HttpStatusCode && key != CommonTransportKeys.HttpReasonPhrase)
  245. responseStr.Append(key + ": " + entry.Value.ToString() + "\r\n");
  246. }
  247. }
  248. responseStr.Append("Server: Mono Remoting, Mono CLR " + System.Environment.Version.ToString() + "\r\n");
  249. if(responseStream != null && responseStream.Length!=0)
  250. {
  251. responseStr.Append("Content-Length: "+responseStream.Length.ToString()+"\r\n");
  252. entityBuffer = new byte[responseStream.Length];
  253. responseStream.Seek(0 , SeekOrigin.Begin);
  254. responseStream.Read(entityBuffer,0,entityBuffer.Length);
  255. }
  256. else
  257. responseStr.Append("Content-Length: 0\r\n");
  258. responseStr.Append("X-Powered-By: Mono\r\n");
  259. responseStr.Append("Connection: close\r\n");
  260. responseStr.Append("\r\n");
  261. headersBuffer = Encoding.ASCII.GetBytes (responseStr.ToString());
  262. //send headersBuffer
  263. reqArg.Stream.Write (headersBuffer, 0, headersBuffer.Length);
  264. if (entityBuffer != null)
  265. reqArg.Stream.Write (entityBuffer, 0, entityBuffer.Length);
  266. }
  267. internal static string GetReasonPhrase (int HttpStatusCode)
  268. {
  269. switch (HttpStatusCode)
  270. {
  271. case 100 : return "Continue" ;
  272. case 101 :return "Switching Protocols";
  273. case 200 :return "OK";
  274. case 201 :return "Created";
  275. case 202 :return "Accepted";
  276. case 203 :return "Non-Authoritative Information";
  277. case 204 :return "No Content";
  278. case 205 :return "Reset Content";
  279. case 206 :return "Partial Content";
  280. case 300 :return "Multiple Choices";
  281. case 301 :return "Moved Permanently";
  282. case 302 :return "Found";
  283. case 303 :return "See Other";
  284. case 304 :return "Not Modified";
  285. case 305 :return "Use Proxy";
  286. case 307 :return "Temporary Redirect";
  287. case 400 :return "Bad Request";
  288. case 401 :return "Unauthorized";
  289. case 402 :return "Payment Required";
  290. case 403 :return "Forbidden";
  291. case 404 :return "Not Found";
  292. case 405 :return "Method Not Allowed";
  293. case 406 :return "Not Acceptable";
  294. case 407 :return "Proxy Authentication Required";
  295. case 408 :return "Request Time-out";
  296. case 409 :return "Conflict";
  297. case 410 :return "Gone";
  298. case 411 :return "Length Required";
  299. case 412 :return "Precondition Failed";
  300. case 413 :return "Request Entity Too Large";
  301. case 414 :return "Request-URI Too Large";
  302. case 415 :return "Unsupported Media Type";
  303. case 416 :return "Requested range not satisfiable";
  304. case 417 :return "Expectation Failed";
  305. case 500 :return "Internal Server Error";
  306. case 501 :return "Not Implemented";
  307. case 502 :return "Bad Gateway";
  308. case 503 :return "Service Unavailable";
  309. case 504 :return "Gateway Time-out";
  310. case 505 :return "HTTP Version not supported";
  311. default: return "";
  312. }
  313. }
  314. }
  315. internal sealed class ReqMessageParser
  316. {
  317. private const int nCountReq = 14;
  318. private const int nCountEntity = 15;
  319. private static bool bInitialized = false;
  320. private static String [] ReqRegExpString = new String [nCountReq ];
  321. private static String [] EntityRegExpString = new String[nCountEntity];
  322. private static Regex [] ReqRegExp = new Regex[nCountReq];
  323. private static Regex [] EntityRegExp = new Regex[nCountEntity];
  324. public ReqMessageParser ()
  325. {
  326. }
  327. public static bool ParseHeaderField(string buffer,IDictionary headers)
  328. {
  329. try
  330. {
  331. if(!bInitialized)
  332. {
  333. Initialize();
  334. bInitialized =true;
  335. }
  336. if(IsRequestField(buffer,headers))
  337. return true;
  338. if(IsEntityField(buffer,headers))
  339. return true ;
  340. }
  341. catch(Exception )
  342. {
  343. //<Exception>
  344. }
  345. //Exception
  346. return false;
  347. }
  348. private static bool Initialize()
  349. {
  350. if(bInitialized)
  351. return true;
  352. bInitialized = true;
  353. //initialize array
  354. //Create all the Regular expressions
  355. InitializeRequestRegExp();
  356. InitiazeEntityRegExp();
  357. for(int i=0;i<nCountReq;i++)
  358. ReqRegExp[i] = new Regex(ReqRegExpString[i],RegexOptions.Compiled|RegexOptions.IgnoreCase);
  359. for(int i=0;i<nCountEntity;i++)
  360. EntityRegExp[i] = new Regex(EntityRegExpString[i],RegexOptions.Compiled|RegexOptions.IgnoreCase);
  361. return true;
  362. }
  363. private static void InitializeRequestRegExp()
  364. {
  365. //Request Header Fields
  366. //
  367. ReqRegExpString[0] = "^accept(\\s*:\\s*)(?<accept>\\S+)(\\s*|)(\\s*)$";
  368. ReqRegExpString[1] = "^accept-charset(\\s*:\\s*)(?<accept_charset>\\S+(\\s|\\S)*\\S)(\\s*)$";
  369. ReqRegExpString[2] = "^accept-encoding(\\s*:\\s*)(?<accept_Encoding>\\S+(\\s|\\S)*\\S)(\\s*)$";
  370. ReqRegExpString[3] = "^authorization(\\s*:\\s*)(?<authorization>\\S+(\\s|\\S)*\\S)(\\s*)$";
  371. ReqRegExpString[4] = "^accept-language(\\s*:\\s*)(?<accept_Language>\\S+(\\s|\\S)*\\S)(\\s*)$";
  372. ReqRegExpString[5] = "^from(\\s*:\\s*)(?<from>\\S+(\\s|\\S)*\\S)(\\s*)$";
  373. ReqRegExpString[6] = "^host(\\s*:\\s*)(?<host>\\S+(\\s|\\S)*\\S)(\\s*)$";
  374. ReqRegExpString[7] = "^if-modified-since(\\s*:\\s*)(?<if_modified>\\S+(\\s|\\S)*\\S)(\\s*)$";
  375. ReqRegExpString[8] = "^proxy-authorization(\\s*:\\s*)(?<proxy_auth>\\S+(\\s|\\S)*\\S)(\\s*)$";
  376. ReqRegExpString[9] = "^range(\\s*:\\s*)(?<range>\\S+(\\s|\\S)*\\S)(\\s*)$";
  377. ReqRegExpString[10] = "^user-agent(\\s*:\\s*)(?<user_agent>\\S+(\\s|\\S)*\\S)(\\s*)$";
  378. ReqRegExpString[11] = "^expect(\\s*:\\s*)(?<expect>\\S+(\\s|\\S)*\\S)(\\s*)$";
  379. ReqRegExpString[12] = "^connection(\\s*:\\s*)(?<connection>\\S+(\\s|\\S)*\\S)(\\s*)$";
  380. ReqRegExpString[13] = "^(?<method>\\w+)(\\s+)(?<request_url>\\S+)(\\s+)(?<http_version>\\S+)(\\s*)$";
  381. // ReqRegExpString[14] = "";
  382. }
  383. private static void InitiazeEntityRegExp()
  384. {
  385. EntityRegExpString[0] = "^allow(\\s*:\\s*)(?<allow>[0-9]+)(\\s*)$";
  386. EntityRegExpString[1] = "^content-encoding(\\s*:\\s*)(?<content_encoding>\\S+(\\s|\\S)*\\S)(\\s*)$";
  387. EntityRegExpString[2] = "^content-language(\\s*:\\s*)(?<content_language>\\S+(\\s|\\S)*\\S)(\\s*)$";
  388. EntityRegExpString[3] = "^content-length(\\s*:\\s*)(?<content_length>[0-9]+)(\\s*)$";
  389. EntityRegExpString[4] = "^content-range(\\s*:\\s*)(?<content_range>\\S+(\\s|\\S)*\\S)(\\s*)$";
  390. EntityRegExpString[5] = "^content-type(\\s*:\\s*)(?<content_type>\\S+(\\s|\\S)*\\S)(\\s*)$";
  391. EntityRegExpString[6] = "^content-version(\\s*:\\s*)(?<content_version>\\S+(\\s|\\S)*\\S)(\\s*)$";
  392. EntityRegExpString[7] = "^derived-from(\\s*:\\s*)(?<derived_from>\\S+(\\s|\\S)*\\S)(\\s*)$";
  393. EntityRegExpString[8] = "^expires(\\s*:\\s*)(?<expires>\\S+(\\s|\\S)*\\S)(\\s*)$";//date
  394. EntityRegExpString[9] = "^last-modified(\\s*:\\s*)(?<last_modified>\\S+(\\s|\\S)*\\S)(\\s*)$";//date
  395. EntityRegExpString[10] = "^link(\\s*:\\s*)(?<link>\\S+(\\s|\\S)*\\S)(\\s*)$";
  396. EntityRegExpString[11] = "^title(\\s*:\\s*)(?<title>\\S+(\\s|\\S)*\\S)(\\s*)$";
  397. EntityRegExpString[12] = "^transfere-encoding(\\s*:\\s*)(?<transfere_encoding>\\S+(\\s|\\S)*\\S)(\\s*)$";
  398. EntityRegExpString[13] = "^url-header(\\s*:\\s*)(?<url_header>\\S+(\\s|\\S)*\\S)(\\s*)$";
  399. EntityRegExpString[14] = "^extension-header(\\s*:\\s*)(?<extension_header>\\S+(\\s|\\S)*\\S)(\\s*)$";
  400. }
  401. private static void CopyGroupNames(Regex regEx , Match m , IDictionary headers)
  402. {
  403. if(!m.Success)
  404. return;
  405. string [] ar = regEx.GetGroupNames();
  406. GroupCollection gc = m.Groups;
  407. for(int i=0;i<ar.Length;i++)
  408. {
  409. if(! char.IsLetter(ar[i],0))
  410. continue;
  411. headers.Add(ar[i],gc[ar[i]].Value);
  412. }
  413. }
  414. private static bool IsRequestField(string buffer , IDictionary HeaderItems)
  415. {
  416. if(Request_accept(buffer , HeaderItems))
  417. return true;
  418. if(Request_accept_charset(buffer , HeaderItems))
  419. return true;
  420. if(Request_accept_encoding(buffer , HeaderItems))
  421. return true;
  422. if(Request_accept_language(buffer , HeaderItems))
  423. return true;
  424. if(Request_authorization(buffer , HeaderItems))
  425. return true;
  426. if(Request_connection(buffer , HeaderItems))
  427. return true;
  428. if(Request_expect(buffer , HeaderItems))
  429. return true;
  430. if(Request_from(buffer , HeaderItems))
  431. return true;
  432. if(Request_host(buffer , HeaderItems))
  433. return true;
  434. if(Request_modified(buffer , HeaderItems))
  435. return true;
  436. if(Request_proxy_authorization(buffer , HeaderItems))
  437. return true;
  438. if(Request_user_agent(buffer , HeaderItems))
  439. return true;
  440. if(Request_request_line(buffer , HeaderItems))
  441. return true;
  442. return false;
  443. }
  444. private static bool IsEntityField(string buffer , IDictionary HeaderItems)
  445. {
  446. if(Entity_allow(buffer , HeaderItems))
  447. return true;
  448. if(Entity_content_encoding(buffer , HeaderItems))
  449. return true;
  450. if(Entity_content_language(buffer , HeaderItems))
  451. return true;
  452. if(Entity_content_length(buffer , HeaderItems))
  453. return true;
  454. if(Entity_content_range(buffer , HeaderItems))
  455. return true;
  456. if(Entity_content_type(buffer , HeaderItems))
  457. return true;
  458. if(Entity_content_version(buffer , HeaderItems))
  459. return true;
  460. if(Entity_dervied_from(buffer , HeaderItems))
  461. return true;
  462. if(Entity_expires(buffer , HeaderItems))
  463. return true;
  464. if(Entity_extension_header(buffer , HeaderItems))
  465. return true;
  466. if(Entity_last_modified(buffer , HeaderItems))
  467. return true;
  468. if(Entity_link(buffer , HeaderItems))
  469. return true;
  470. if(Entity_title(buffer , HeaderItems))
  471. return true;
  472. if(Entity_transfere_encoding(buffer , HeaderItems))
  473. return true;
  474. if(Entity_url_header(buffer , HeaderItems))
  475. return true;
  476. return false;
  477. }
  478. public static bool IsCustomHeader(string buffer,IDictionary CustomHeader)
  479. {
  480. Regex CustomHeaderEx = new Regex("^(?<header>\\S+)(\\s*:\\s*)(?<field>\\S+(\\s|\\S)*\\S)(\\s*)",RegexOptions.Compiled);
  481. Match m = CustomHeaderEx.Match(buffer);
  482. if(!m.Success)
  483. return false;
  484. CustomHeader.Add(m.Groups["header"].Value,m.Groups["field"].Value);
  485. return true;
  486. }
  487. //********************************************************
  488. //REQUEST
  489. private static bool Request_accept(string buffer,IDictionary HeaderItems)
  490. {
  491. Match m = ReqRegExp[0].Match(buffer);
  492. if(!m.Success)
  493. return false;
  494. HeaderItems.Add("accept",m.Groups["accept"].Value);
  495. return true;
  496. }
  497. private static bool Request_accept_charset(string buffer,IDictionary HeaderItems)
  498. {
  499. Match m = ReqRegExp[1].Match(buffer);
  500. if(!m.Success)
  501. return false;
  502. HeaderItems.Add("accept-charset",m.Groups["accept_charset"].Value);
  503. return true;
  504. }
  505. private static bool Request_accept_encoding(string buffer,IDictionary HeaderItems)
  506. {
  507. Match m = ReqRegExp[2].Match(buffer);
  508. if(!m.Success)
  509. return false;
  510. HeaderItems.Add("accept-encoding",m.Groups["accept_encoding"].Value);
  511. return true;
  512. }
  513. private static bool Request_authorization(string buffer,IDictionary HeaderItems)
  514. {
  515. Match m = ReqRegExp[3].Match(buffer);
  516. if(!m.Success)
  517. return false;
  518. HeaderItems.Add("authorization",m.Groups["authorization"].Value);
  519. return true;
  520. }
  521. private static bool Request_accept_language(string buffer,IDictionary HeaderItems)
  522. {
  523. Match m = ReqRegExp[4].Match(buffer);
  524. if(!m.Success)
  525. return false;
  526. HeaderItems.Add("accept-language",m.Groups["accept_language"].Value);
  527. return true;
  528. }
  529. private static bool Request_from(string buffer,IDictionary HeaderItems)
  530. {
  531. Match m = ReqRegExp[5].Match(buffer);
  532. if(!m.Success)
  533. return false;
  534. HeaderItems.Add("from",m.Groups["from"].Value);
  535. return true;
  536. }
  537. private static bool Request_host(string buffer,IDictionary HeaderItems)
  538. {
  539. Match m = ReqRegExp[6].Match(buffer);
  540. if(!m.Success)
  541. return false;
  542. HeaderItems.Add("host",m.Groups["host"].Value);
  543. return true;
  544. }
  545. private static bool Request_modified(string buffer,IDictionary HeaderItems)
  546. {
  547. Match m = ReqRegExp[7].Match(buffer);
  548. if(!m.Success)
  549. return false;
  550. HeaderItems.Add("modified",m.Groups["modified"].Value);
  551. return true;
  552. }
  553. private static bool Request_proxy_authorization(string buffer,IDictionary HeaderItems)
  554. {
  555. Match m = ReqRegExp[8].Match(buffer);
  556. if(!m.Success)
  557. return false;
  558. HeaderItems.Add("proxy-authorization",m.Groups["proxy_authorization"].Value);
  559. return true;
  560. }
  561. private static bool Request_range(string buffer , IDictionary HeaderItems)
  562. {
  563. Match m = ReqRegExp[9].Match(buffer);
  564. if(!m.Success)
  565. return false;
  566. HeaderItems.Add("range",m.Groups["range"].Value);
  567. return true;
  568. }
  569. private static bool Request_user_agent(string buffer,IDictionary HeaderItems)
  570. {
  571. Match m = ReqRegExp[10].Match(buffer);
  572. if(!m.Success)
  573. return false;
  574. HeaderItems.Add("user-agent",m.Groups["user_agent"].Value);
  575. return true;
  576. }
  577. private static bool Request_expect(string buffer,IDictionary HeaderItems)
  578. {
  579. Match m = ReqRegExp[11].Match(buffer);
  580. if(!m.Success)
  581. return false;
  582. HeaderItems.Add("expect",m.Groups["expect"].Value);
  583. return true;
  584. }
  585. private static bool Request_connection(string buffer,IDictionary HeaderItems)
  586. {
  587. Match m = ReqRegExp[12].Match(buffer);
  588. if(!m.Success)
  589. return false;
  590. HeaderItems.Add("connection",m.Groups["connection"].Value);
  591. return true;
  592. }
  593. private static bool Request_request_line(string buffer, IDictionary HeaderItems)
  594. {
  595. Match m = ReqRegExp[13].Match(buffer);
  596. if(!m.Success)
  597. return false;
  598. //ReqRegExpString[13] = "(?<method>\\w+)(\\s+)(?<request_url>\\S+)(\\s+)(?<http_version>\\S+)";
  599. HeaderItems.Add("method",m.Groups["method"].Value);
  600. HeaderItems.Add("request-url",m.Groups["request_url"].Value);
  601. HeaderItems.Add("http-version",m.Groups["http_version"].Value);
  602. return true;
  603. }
  604. //********************************************************
  605. //********************************************************
  606. //ENTITY
  607. private static bool Entity_allow(string buffer,IDictionary HeaderItems)
  608. {
  609. Match m = EntityRegExp[0].Match(buffer);
  610. if(!m.Success)
  611. return false;
  612. HeaderItems.Add("allow",m.Groups["allow"].Value);
  613. return true;
  614. }
  615. private static bool Entity_content_encoding(string buffer,IDictionary HeaderItems)
  616. {
  617. Match m = EntityRegExp[1].Match(buffer);
  618. if(!m.Success)
  619. return false;
  620. HeaderItems.Add("content-encoding",m.Groups["content_encoding"].Value);
  621. return true;
  622. }
  623. private static bool Entity_content_language(string buffer,IDictionary HeaderItems)
  624. {
  625. Match m = EntityRegExp[2].Match(buffer);
  626. if(!m.Success)
  627. return false;
  628. HeaderItems.Add("content-language",m.Groups["content_language"].Value);
  629. return true;
  630. }
  631. private static bool Entity_content_length(string buffer,IDictionary HeaderItems)
  632. {
  633. Match m = EntityRegExp[3].Match(buffer);
  634. if(!m.Success)
  635. return false;
  636. int length;
  637. try
  638. {
  639. length = Int32.Parse(m.Groups["content_length"].ToString());
  640. }
  641. catch (Exception )
  642. {
  643. //<Exception>
  644. return false;
  645. }
  646. HeaderItems.Add("content-length",length);
  647. return true;
  648. }
  649. private static bool Entity_content_range(string buffer,IDictionary HeaderItems)
  650. {
  651. Match m = EntityRegExp[4].Match(buffer);
  652. if(!m.Success)
  653. return false;
  654. HeaderItems.Add("content-range",m.Groups["content_range"].Value);
  655. return true;
  656. }
  657. private static bool Entity_content_type(string buffer,IDictionary HeaderItems)
  658. {
  659. Match m = EntityRegExp[5].Match(buffer);
  660. if(!m.Success)
  661. return false;
  662. HeaderItems.Add("content-type",m.Groups["content_type"].Value);
  663. return true;
  664. }
  665. private static bool Entity_content_version(string buffer,IDictionary HeaderItems)
  666. {
  667. Match m = EntityRegExp[6].Match(buffer);
  668. if(!m.Success)
  669. return false;
  670. HeaderItems.Add("content-version",m.Groups["content_version"].Value);
  671. return true;
  672. }
  673. private static bool Entity_dervied_from(string buffer,IDictionary HeaderItems)
  674. {
  675. Match m = EntityRegExp[7].Match(buffer);
  676. if(!m.Success)
  677. return false;
  678. HeaderItems.Add("dervied-from",m.Groups["dervied_from"].Value);
  679. return true;
  680. }
  681. private static bool Entity_expires(string buffer,IDictionary HeaderItems)
  682. {
  683. Match m = EntityRegExp[8].Match(buffer);
  684. if(!m.Success)
  685. return false;
  686. HeaderItems.Add("expires",m.Groups["expires"].Value);
  687. return true;
  688. }
  689. private static bool Entity_last_modified(string buffer,IDictionary HeaderItems)
  690. {
  691. Match m = EntityRegExp[9].Match(buffer);
  692. if(!m.Success)
  693. return false;
  694. HeaderItems.Add("last-modified",m.Groups["last_modified"].Value);
  695. return true;
  696. }
  697. private static bool Entity_link(string buffer,IDictionary HeaderItems)
  698. {
  699. Match m = EntityRegExp[10].Match(buffer);
  700. if(!m.Success)
  701. return false;
  702. HeaderItems.Add("link",m.Groups["link"].Value);
  703. return true;
  704. }
  705. private static bool Entity_title(string buffer,IDictionary HeaderItems)
  706. {
  707. Match m = EntityRegExp[11].Match(buffer);
  708. if(!m.Success)
  709. return false;
  710. HeaderItems.Add("title",m.Groups["title"].Value);
  711. return true;
  712. }
  713. private static bool Entity_transfere_encoding(string buffer,IDictionary HeaderItems)
  714. {
  715. Match m = EntityRegExp[12].Match(buffer);
  716. if(!m.Success)
  717. return false;
  718. HeaderItems.Add("transfere-encoding",m.Groups["transfere_encoding"].Value);
  719. return true;
  720. }
  721. private static bool Entity_url_header(string buffer,IDictionary HeaderItems)
  722. {
  723. Match m = EntityRegExp[13].Match(buffer);
  724. if(!m.Success)
  725. return false;
  726. HeaderItems.Add("url-header",m.Groups["url_header"].Value);
  727. return true;
  728. }
  729. private static bool Entity_extension_header(string buffer,IDictionary HeaderItems)
  730. {
  731. Match m = EntityRegExp[14].Match(buffer);
  732. if(!m.Success)
  733. return false;
  734. HeaderItems.Add("extension-header",m.Groups["extension_header"].Value);
  735. return true;
  736. }
  737. //********************************************************
  738. }
  739. }