HttpServer.cs 25 KB

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