HttpRequest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. //
  2. // System.Web.HttpRequest
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. using System.IO;
  11. using System.Text;
  12. namespace System.Web {
  13. //-- Methods from HttpRequest not implemented
  14. // public HttpFileCollection Files {get;}
  15. // public HttpBrowserCapabilities Browser {get; set;}
  16. // public HttpClientCertificate ClientCertificate {get;}
  17. // public string ApplicationPath {get;}
  18. // public HttpCookieCollection Cookies {get;}
  19. // public Stream Filter {get; set;}
  20. // public string MapPath(string virtualPath, string baseVirtualDir, bool allowCrossAppMapping);
  21. // public string MapPath(string virtualPath);
  22. // public int [] MapImageCoordinates(string imageFieldName);
  23. // public string FilePath {get;}
  24. // public Uri UrlReferrer
  25. [MonoTODO("Review security in all path access function")]
  26. public class HttpRequest {
  27. private string [] _arrAcceptTypes;
  28. private string [] _arrUserLanguages;
  29. private byte [] _arrRawContent;
  30. private int _iContentLength;
  31. private string _sContentType;
  32. private string _sHttpMethod;
  33. private string _sRawUrl;
  34. private string _sUserAgent;
  35. private string _sUserHostAddress;
  36. private string _sUserHostName;
  37. private string _sPath;
  38. private string _sPathInfo;
  39. private string _sFilePath;
  40. private string _sPathTranslated;
  41. private string _sQueryStringRaw;
  42. private string _sRequestType;
  43. private string _sRequestRootVirtualDir;
  44. private Encoding _oContentEncoding;
  45. private Uri _oUriReferrer;
  46. private Uri _oUrl;
  47. private int _iTotalBytes;
  48. private HttpContext _oContext;
  49. private HttpWorkerRequest _WorkerRequest;
  50. private HttpRequestStream _oInputStream;
  51. private HttpValueCollection _oServerVariables;
  52. private HttpValueCollection _oHeaders;
  53. private HttpValueCollection _oQueryString;
  54. private HttpValueCollection _oFormData;
  55. private HttpValueCollection _oParams;
  56. public HttpRequest(string Filename, string Url, string Querystring) {
  57. _iContentLength = -1;
  58. _iTotalBytes = -1;
  59. _WorkerRequest = null;
  60. _sPathTranslated = Filename;
  61. _sRequestType = "GET";
  62. _sHttpMethod = "GET";
  63. _oUrl = new Uri(Url);
  64. _sPath = _oUrl.AbsolutePath;
  65. _sQueryStringRaw = Querystring;
  66. _oQueryString = new HttpValueCollection(Querystring, true, Encoding.Default);
  67. }
  68. internal HttpRequest(HttpWorkerRequest WorkRequest, HttpContext Context) {
  69. _WorkerRequest = WorkRequest;
  70. _oContext = Context;
  71. _iContentLength = -1;
  72. _iTotalBytes = -1;
  73. }
  74. static private string MakeServerVariableFromHeader(string header) {
  75. return "HTTP_" + header.ToUpper().Replace("-", "_");
  76. }
  77. [MonoTODO("Need to support non-raw mode also..")]
  78. private string GetAllHeaders(bool Raw) {
  79. StringBuilder oData;
  80. if (null == _WorkerRequest) {
  81. return null;
  82. }
  83. oData = new StringBuilder(512);
  84. string sHeaderValue;
  85. string sHeaderName;
  86. int iCount = 0;
  87. // Add all know headers
  88. for (; iCount != 40; iCount++) {
  89. sHeaderValue = _WorkerRequest.GetKnownRequestHeader(iCount);
  90. if (null != sHeaderValue && sHeaderValue.Length > 0) {
  91. sHeaderName = _WorkerRequest.GetKnownRequestHeader(iCount);
  92. if (null != sHeaderName && sHeaderName.Length > 0) {
  93. oData.Append(sHeaderName);
  94. oData.Append(": ");
  95. oData.Append(sHeaderValue);
  96. oData.Append("\r\n");
  97. }
  98. }
  99. }
  100. // Get all other headers
  101. string [][] arrUnknownHeaders = _WorkerRequest.GetUnknownRequestHeaders();
  102. if (null != arrUnknownHeaders) {
  103. for (iCount = 0; iCount != arrUnknownHeaders.Length; iCount++) {
  104. oData.Append(arrUnknownHeaders[iCount][0]);
  105. oData.Append(": ");
  106. oData.Append(arrUnknownHeaders[iCount][1]);
  107. oData.Append("\r\n");
  108. }
  109. }
  110. return oData.ToString();
  111. }
  112. [MonoTODO("We need to handly 'dynamic' variables like AUTH_USER, that can be changed during runtime... special collection")]
  113. private void ParseServerVariables() {
  114. if (null == _WorkerRequest) {
  115. return;
  116. }
  117. if (_oServerVariables == null) {
  118. string sTmp;
  119. _oServerVariables = new HttpValueCollection();
  120. _oServerVariables.Add("ALL_HTTP", GetAllHeaders(false));
  121. _oServerVariables.Add("ALL_RAW", GetAllHeaders(true));
  122. _oServerVariables.Add("APPL_MD_PATH", _WorkerRequest.GetServerVariable("APPL_MD_PATH"));
  123. _oServerVariables.Add("AUTH_PASSWORD", _WorkerRequest.GetServerVariable("AUTH_PASSWORD"));
  124. _oServerVariables.Add("CERT_COOKIE", _WorkerRequest.GetServerVariable("CERT_COOKIE"));
  125. _oServerVariables.Add("CERT_FLAGS", _WorkerRequest.GetServerVariable("CERT_FLAGS"));
  126. _oServerVariables.Add("CERT_ISSUER", _WorkerRequest.GetServerVariable("CERT_ISSUER"));
  127. _oServerVariables.Add("CERT_KEYSIZE", _WorkerRequest.GetServerVariable("CERT_KEYSIZE"));
  128. _oServerVariables.Add("CERT_SECRETKEYSIZE", _WorkerRequest.GetServerVariable("CERT_SECRETKEYSIZE"));
  129. _oServerVariables.Add("CERT_SERIALNUMBER", _WorkerRequest.GetServerVariable("CERT_SERIALNUMBER"));
  130. _oServerVariables.Add("CERT_SERVER_ISSUER", _WorkerRequest.GetServerVariable("CERT_SERVER_ISSUER"));
  131. _oServerVariables.Add("CERT_SERVER_SUBJECT", _WorkerRequest.GetServerVariable("CERT_SERVER_SUBJECT"));
  132. _oServerVariables.Add("CERT_SUBJECT", _WorkerRequest.GetServerVariable("CERT_SUBJECT"));
  133. _oServerVariables.Add("GATEWAY_INTERFACE", _WorkerRequest.GetServerVariable("GATEWAY_INTERFACE"));
  134. _oServerVariables.Add("HTTPS", _WorkerRequest.GetServerVariable("HTTPS"));
  135. _oServerVariables.Add("HTTPS_KEYSIZE", _WorkerRequest.GetServerVariable("HTTPS_KEYSIZE"));
  136. _oServerVariables.Add("HTTPS_SECRETKEYSIZE", _WorkerRequest.GetServerVariable("HTTPS_SECRETKEYSIZE"));
  137. _oServerVariables.Add("CONTENT_TYPE", ContentType);
  138. _oServerVariables.Add("HTTPS_SERVER_ISSUER", _WorkerRequest.GetServerVariable("HTTPS_SERVER_ISSUER"));
  139. _oServerVariables.Add("HTTPS_SERVER_SUBJECT", _WorkerRequest.GetServerVariable("HTTPS_SERVER_SUBJECT"));
  140. _oServerVariables.Add("INSTANCE_ID", _WorkerRequest.GetServerVariable("INSTANCE_ID"));
  141. _oServerVariables.Add("INSTANCE_META_PATH", _WorkerRequest.GetServerVariable("INSTANCE_META_PATH"));
  142. _oServerVariables.Add("LOCAL_ADDR", _WorkerRequest.GetLocalAddress());
  143. _oServerVariables.Add("REMOTE_ADDR", UserHostAddress);
  144. _oServerVariables.Add("REMOTE_HOST", UserHostName);
  145. _oServerVariables.Add("REQUEST_METHOD", HttpMethod);
  146. _oServerVariables.Add("SERVER_NAME", _WorkerRequest.GetServerName());
  147. _oServerVariables.Add("SERVER_PORT", _WorkerRequest.GetLocalPort().ToString());
  148. _oServerVariables.Add("SERVER_PROTOCOL", _WorkerRequest.GetHttpVersion());
  149. _oServerVariables.Add("SERVER_SOFTWARE", _WorkerRequest.GetServerVariable("SERVER_SOFTWARE"));
  150. if (_WorkerRequest.IsSecure()) {
  151. _oServerVariables.Add("SERVER_PORT_SECURE", "1");
  152. } else {
  153. _oServerVariables.Add("SERVER_PORT_SECURE", "0");
  154. }
  155. sTmp = _WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength);
  156. if (null != sTmp) {
  157. _oServerVariables.Add("CONTENT_LENGTH", sTmp);
  158. }
  159. // TODO: Should be dynamic
  160. if (null != _oContext.User && _oContext.User.Identity.IsAuthenticated) {
  161. _oServerVariables.Add("AUTH_TYPE", _oContext.User.Identity.AuthenticationType);
  162. _oServerVariables.Add("AUTH_USER", _oContext.User.Identity.Name);
  163. } else {
  164. _oServerVariables.Add("AUTH_TYPE", "");
  165. _oServerVariables.Add("AUTH_USER", "");
  166. }
  167. _oServerVariables.Add("PATH_INFO", PathInfo);
  168. _oServerVariables.Add("PATH_TRANSLATED", PhysicalPath);
  169. _oServerVariables.Add("QUERY_STRING", QueryStringRaw);
  170. _oServerVariables.Add("SCRIPT_NAME", FilePath);
  171. // end dynamic
  172. _oServerVariables.MakeReadOnly();
  173. }
  174. }
  175. [MonoTODO("Handle Multipart data also, content-encoding check")]
  176. private void ParseFormData() {
  177. if (_oFormData == null) {
  178. byte [] arrData = GetRawContent();
  179. _oFormData = new HttpValueCollection(ContentEncoding.GetString(arrData), true, ContentEncoding);
  180. }
  181. }
  182. void Dispose() {
  183. }
  184. [MonoTODO("Handle Multipart data, max content length?")]
  185. private byte [] GetRawContent() {
  186. if (null == _arrRawContent) {
  187. if (null == _WorkerRequest) {
  188. return null;
  189. }
  190. // TODO: Check max length?
  191. _arrRawContent = _WorkerRequest.GetPreloadedEntityBody();
  192. if (!_WorkerRequest.IsEntireEntityBodyIsPreloaded()) {
  193. byte [] arrTemp;
  194. byte [] arrBuffer;
  195. arrBuffer = new byte[16384];
  196. int iLoaded = 16384;
  197. while (iLoaded == arrBuffer.Length) {
  198. iLoaded = _WorkerRequest.ReadEntityBody(arrBuffer, arrBuffer.Length);
  199. // Save data
  200. arrTemp = new byte[_arrRawContent.Length + iLoaded];
  201. Array.Copy(_arrRawContent, 0, arrTemp, 0, _arrRawContent.Length);
  202. Array.Copy(arrBuffer, 0, arrTemp, _arrRawContent.Length, iLoaded);
  203. _arrRawContent = arrTemp;
  204. }
  205. }
  206. }
  207. return _arrRawContent;
  208. }
  209. public string [] AcceptTypes {
  210. get {
  211. if (null == _arrAcceptTypes && null != _WorkerRequest) {
  212. _arrAcceptTypes = HttpHelper.ParseMultiValueHeader(_WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderAccept));
  213. }
  214. return _arrAcceptTypes;
  215. }
  216. }
  217. public string ApplicationPath {
  218. get {
  219. if (null != _WorkerRequest) {
  220. return _WorkerRequest.GetAppPath();
  221. }
  222. return null;
  223. }
  224. }
  225. [MonoTODO()]
  226. public HttpBrowserCapabilites Browser {
  227. get {
  228. throw new NotImplementedException();
  229. }
  230. set {
  231. throw new NotImplementedException();
  232. }
  233. }
  234. [MonoTODO()]
  235. public HttpClientCertificate ClientCertificate {
  236. get {
  237. throw new NotImplementedException();
  238. }
  239. }
  240. [MonoTODO("Get content encoding from Syste.Web.Configuration namespace")]
  241. public Encoding ContentEncoding {
  242. get {
  243. if (_oContentEncoding == null) {
  244. // TODO: Get from config what is the default encoding
  245. // TODO: Should we get encoding from HttpHeaders? Just get charset from ContentType and the get encoding..
  246. _oContentEncoding = Encoding.Default;
  247. }
  248. return _oContentEncoding;
  249. }
  250. set {
  251. _oContentEncoding = value;
  252. }
  253. }
  254. public int ContentLength {
  255. get {
  256. if (_iContentLength == -1 && null != _WorkerRequest) {
  257. string sLength = _WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength);
  258. if (sLength != null) {
  259. try {
  260. _iContentLength = Int32.Parse(sLength);
  261. }
  262. catch(Exception) {
  263. }
  264. }
  265. }
  266. if (_iContentLength < 0) {
  267. _iContentLength = 0;
  268. }
  269. return _iContentLength;
  270. }
  271. }
  272. public string ContentType {
  273. get {
  274. if (null == _sContentType) {
  275. if (null != _WorkerRequest) {
  276. _sContentType = _WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentType);
  277. }
  278. if (null == _sContentType) {
  279. _sContentType = string.Empty;
  280. }
  281. }
  282. return _sContentType;
  283. }
  284. }
  285. [MonoTODO()]
  286. public HttpCookieCollection Cookies {
  287. get {
  288. throw new NotImplementedException();
  289. }
  290. }
  291. [MonoTODO()]
  292. public string CurrentExecutionFilePath {
  293. get {
  294. throw new NotImplementedException();
  295. }
  296. }
  297. public string FilePath {
  298. get {
  299. if (null == _sFilePath && null != _WorkerRequest) {
  300. _sFilePath = _WorkerRequest.GetFilePath();
  301. }
  302. return _sFilePath;
  303. }
  304. }
  305. [MonoTODO()]
  306. public HttpFileCollection Files {
  307. get {
  308. throw new NotImplementedException();
  309. }
  310. }
  311. [MonoTODO("Use stream filter in the request stream")]
  312. public Stream Filter {
  313. get {
  314. throw new NotImplementedException();
  315. }
  316. set {
  317. throw new NotImplementedException();
  318. }
  319. }
  320. public NameValueCollection Form {
  321. get {
  322. ParseFormData();
  323. return (NameValueCollection) _oFormData;
  324. }
  325. }
  326. public NameValueCollection Headers {
  327. get {
  328. if (_oHeaders == null) {
  329. _oHeaders = new HttpValueCollection();
  330. if (null != _WorkerRequest) {
  331. string sHeaderValue;
  332. string sHeaderName;
  333. int iCount = 0;
  334. // Add all know headers
  335. for (; iCount != 40; iCount++) {
  336. sHeaderValue = _WorkerRequest.GetKnownRequestHeader(iCount);
  337. if (null != sHeaderValue && sHeaderValue.Length > 0) {
  338. sHeaderName = _WorkerRequest.GetKnownRequestHeader(iCount);
  339. if (null != sHeaderName && sHeaderName.Length > 0) {
  340. _oHeaders.Add(sHeaderName, sHeaderValue);
  341. }
  342. }
  343. }
  344. // Get all other headers
  345. string [][] arrUnknownHeaders = _WorkerRequest.GetUnknownRequestHeaders();
  346. if (null != arrUnknownHeaders) {
  347. for (iCount = 0; iCount != arrUnknownHeaders.Length; iCount++) {
  348. _oHeaders.Add(arrUnknownHeaders[iCount][0], arrUnknownHeaders[iCount][1]);
  349. }
  350. }
  351. }
  352. // Make headers read-only
  353. _oHeaders.MakeReadOnly();
  354. }
  355. return (NameValueCollection) _oHeaders;
  356. }
  357. }
  358. public string HttpMethod {
  359. get {
  360. if (null == _sHttpMethod) {
  361. if (null != _WorkerRequest) {
  362. _sHttpMethod = _WorkerRequest.GetHttpVerbName().ToUpper();
  363. }
  364. if (_sHttpMethod == null) {
  365. _sHttpMethod = "GET";
  366. }
  367. }
  368. return _sHttpMethod;
  369. }
  370. }
  371. public Stream InputStream {
  372. get {
  373. if (_oInputStream == null) {
  374. byte [] arrInputData = GetRawContent();
  375. if (null != arrInputData) {
  376. _oInputStream = new HttpRequestStream(arrInputData, 0, arrInputData.Length);
  377. } else {
  378. _oInputStream = new HttpRequestStream(null, 0, 0);
  379. }
  380. }
  381. return _oInputStream;
  382. }
  383. }
  384. public bool IsAuthenticated {
  385. get {
  386. if (_oContext != null && _oContext.User != null && _oContext.User.Identity != null) {
  387. return _oContext.User.Identity.IsAuthenticated;
  388. }
  389. return false;
  390. }
  391. }
  392. public bool IsSecureConnection {
  393. get {
  394. if (null != _WorkerRequest) {
  395. return _WorkerRequest.IsSecure();
  396. }
  397. return false;
  398. }
  399. }
  400. [MonoTODO("Call item in querystring, form, cookie and servervariables")]
  401. public string this [string sKey] {
  402. get {
  403. throw new NotImplementedException();
  404. }
  405. }
  406. [MonoTODO("Add cookie collection to our Params collection via merge")]
  407. public NameValueCollection Params {
  408. get {
  409. if (_oParams == null) {
  410. _oParams = new HttpValueCollection();
  411. _oParams.Merge(QueryString);
  412. _oParams.Merge(Form);
  413. _oParams.Merge(ServerVariables);
  414. // TODO: Cookie
  415. _oParams.MakeReadOnly();
  416. }
  417. return (NameValueCollection) _oParams;
  418. }
  419. }
  420. public string Path {
  421. get {
  422. if (_sPath == null) {
  423. if (null != _WorkerRequest) {
  424. _sPath = _WorkerRequest.GetUriPath();
  425. }
  426. if (_sPath == null) {
  427. _sPath = string.Empty;
  428. }
  429. }
  430. return _sPath;
  431. }
  432. }
  433. public string PathInfo {
  434. get {
  435. if (_sPathInfo == null) {
  436. if (null != _WorkerRequest) {
  437. _sPathInfo = _WorkerRequest.GetPathInfo();
  438. }
  439. if (_sPathInfo == null) {
  440. _sPathInfo = string.Empty;
  441. }
  442. }
  443. return _sPathInfo;
  444. }
  445. }
  446. public string PhysicalApplicationPath {
  447. get {
  448. if (null != _WorkerRequest) {
  449. return _WorkerRequest.GetAppPathTranslated();
  450. }
  451. return null;
  452. }
  453. }
  454. public string PhysicalPath {
  455. get {
  456. if (null != _WorkerRequest) {
  457. _sPathTranslated = _WorkerRequest.GetFilePathTranslated();
  458. if (null == _sPathTranslated) {
  459. _sPathTranslated = _WorkerRequest.MapPath(FilePath);
  460. }
  461. }
  462. return _sPathTranslated;
  463. }
  464. }
  465. public NameValueCollection QueryString {
  466. get {
  467. if (_oQueryString == null) {
  468. _oQueryString = new HttpValueCollection(QueryStringRaw, true, Encoding.UTF8);
  469. }
  470. return _oQueryString;
  471. }
  472. }
  473. // Used to parse the querystring
  474. internal string QueryStringRaw {
  475. get {
  476. if (_sQueryStringRaw == null && null != _WorkerRequest) {
  477. byte [] arrQuerystringBytes = _WorkerRequest.GetQueryStringRawBytes();
  478. if (null != arrQuerystringBytes && arrQuerystringBytes.Length > 0) {
  479. _sQueryStringRaw = ContentEncoding.GetString(arrQuerystringBytes);
  480. } else {
  481. _sQueryStringRaw = _WorkerRequest.GetQueryString();
  482. }
  483. }
  484. if (_sQueryStringRaw == null) {
  485. _sQueryStringRaw = string.Empty;
  486. }
  487. return _sQueryStringRaw;
  488. }
  489. }
  490. public string RawUrl {
  491. get {
  492. if (null == _sRawUrl) {
  493. if (null != _WorkerRequest) {
  494. _sRawUrl = _WorkerRequest.GetRawUrl();
  495. } else {
  496. _sRawUrl = Path;
  497. if (QueryStringRaw != null && QueryStringRaw.Length > 0) {
  498. _sRawUrl = _sRawUrl + "?" + QueryStringRaw;
  499. }
  500. }
  501. }
  502. return _sRawUrl;
  503. }
  504. }
  505. public string RequestType {
  506. get {
  507. if (null == _sRequestType) {
  508. return HttpMethod;
  509. }
  510. return _sRequestType;
  511. }
  512. }
  513. public NameValueCollection ServerVariables {
  514. get {
  515. ParseServerVariables();
  516. return (NameValueCollection) _oServerVariables;
  517. }
  518. }
  519. public int TotalBytes {
  520. get {
  521. if (_iTotalBytes == -1) {
  522. if (null != InputStream) {
  523. _iTotalBytes = (int) InputStream.Length;
  524. } else {
  525. _iTotalBytes = 0;
  526. }
  527. }
  528. return _iTotalBytes;
  529. }
  530. }
  531. public Uri Url {
  532. get {
  533. if (null == _oUrl) {
  534. _oUrl = new Uri(RawUrl);
  535. }
  536. return _oUrl;
  537. }
  538. }
  539. public Uri UrlReferrer {
  540. get {
  541. if (null == _oUriReferrer && null != _WorkerRequest) {
  542. string sReferrer = _WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderReferer);
  543. if (null != sReferrer && sReferrer.Length > 0) {
  544. try {
  545. if (sReferrer.IndexOf("://") >= 0) {
  546. _oUriReferrer = new Uri(sReferrer);
  547. } else {
  548. _oUriReferrer = new Uri(this.Url, sReferrer);
  549. }
  550. }
  551. catch (Exception) {
  552. }
  553. }
  554. }
  555. return _oUriReferrer;
  556. }
  557. }
  558. public string UserAgent {
  559. get {
  560. if (_sUserAgent == null && _WorkerRequest != null) {
  561. _sUserAgent = _WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderUserAgent);
  562. }
  563. if (_sUserAgent == null) {
  564. _sUserAgent = string.Empty;
  565. }
  566. return _sUserAgent;
  567. }
  568. }
  569. public string UserHostAddress {
  570. get {
  571. if (_sUserHostAddress == null && null != _WorkerRequest) {
  572. _sUserHostAddress = _WorkerRequest.GetRemoteAddress();
  573. }
  574. if (_sUserHostAddress == null || _sUserHostAddress.Length == 0) {
  575. _sUserHostAddress = "127.0.0.1";
  576. }
  577. return _sUserHostAddress;
  578. }
  579. }
  580. public string UserHostName {
  581. get {
  582. if (_sUserHostName == null && null != _WorkerRequest) {
  583. _sUserHostName = _WorkerRequest.GetRemoteName();
  584. }
  585. if (_sUserHostName == null || _sUserHostName.Length == 0) {
  586. _sUserHostName = UserHostAddress;
  587. }
  588. return _sUserHostName;
  589. }
  590. }
  591. public string [] UserLanguages {
  592. get {
  593. if (_arrUserLanguages == null && null != _WorkerRequest) {
  594. _arrUserLanguages = HttpHelper.ParseMultiValueHeader(_WorkerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderAcceptLanguage));
  595. }
  596. return _arrUserLanguages;
  597. }
  598. }
  599. public byte [] BinaryRead(int count) {
  600. int iSize = TotalBytes;
  601. if (iSize == 0) {
  602. throw new ArgumentException();
  603. }
  604. byte [] arrData = new byte[iSize];
  605. int iRetSize = InputStream.Read(arrData, 0, iSize);
  606. if (iRetSize != iSize) {
  607. byte [] tmpData = new byte[iRetSize];
  608. if (iRetSize > 0) {
  609. Array.Copy(arrData, 0, tmpData, 0, iRetSize);
  610. }
  611. arrData = tmpData;
  612. }
  613. return arrData;
  614. }
  615. public int [] MapImageCoordinates(string ImageFieldName) {
  616. NameValueCollection oItems;
  617. if (HttpMethod == "GET" || HttpMethod == "HEAD") {
  618. oItems = QueryString;
  619. } else if (HttpMethod == "POST") {
  620. oItems = Form;
  621. } else {
  622. return null;
  623. }
  624. int [] arrRet = null;
  625. try {
  626. string sX = oItems.Get(ImageFieldName + ".x");
  627. string sY = oItems.Get(ImageFieldName + ".y");
  628. if (null != sX && null != sY) {
  629. int [] arrTmp = new Int32[2];
  630. arrRet[0] = Int32.Parse(sX);
  631. arrRet[1] = Int32.Parse(sY);
  632. arrRet = arrTmp;
  633. }
  634. }
  635. catch (Exception) {
  636. }
  637. return arrRet;
  638. }
  639. public string MapPath(string VirtualPath) {
  640. if (null == _sRequestRootVirtualDir) {
  641. if (null == FilePath || FilePath.Length == 0) {
  642. throw new ArgumentException("Filepath can't be empty");
  643. }
  644. if (FilePath[0] != '/') {
  645. throw new ArgumentException("Filepath must be a root");
  646. }
  647. _sRequestRootVirtualDir = FilePath.Substring(0, FilePath.LastIndexOf('/'));
  648. if (_sRequestRootVirtualDir.Length == 0) {
  649. _sRequestRootVirtualDir = "/";
  650. }
  651. }
  652. return MapPath(VirtualPath, _sRequestRootVirtualDir, true);
  653. }
  654. [MonoTODO("Build a path to send to MapPath in the workerrequest")]
  655. public string MapPath(string virtualPath, string baseVirtualDir, bool allowCrossAppMapping) {
  656. throw new NotImplementedException();
  657. }
  658. public void SaveAs(string filename, bool includeHeaders) {
  659. FileStream oFile;
  660. TextWriter oWriter;
  661. HttpRequestStream oData;
  662. oFile = new FileStream(filename, FileMode.CreateNew);
  663. if (includeHeaders) {
  664. oWriter = new StreamWriter(oFile);
  665. oWriter.Write(HttpMethod + " " + Path);
  666. if (QueryStringRaw != null && QueryStringRaw.Length > 0)
  667. oWriter.Write("?" + QueryStringRaw);
  668. if (_WorkerRequest != null) {
  669. oWriter.Write(" " + _WorkerRequest.GetHttpVersion() + "\r\n");
  670. oWriter.Write(GetAllHeaders(true));
  671. } else {
  672. oWriter.Write("\r\n");
  673. }
  674. oWriter.Write("\r\n");
  675. oWriter.Flush();
  676. }
  677. oData = (HttpRequestStream) InputStream;
  678. if (oData.DataLength > 0) {
  679. oFile.Write(oData.Data, oData.DataOffset, oData.DataLength);
  680. }
  681. oFile.Flush();
  682. oFile.Close();
  683. }
  684. }
  685. }