2
0

HttpServer.cs 25 KB

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