HttpServer.cs 26 KB

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