RequestMono.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using MediaBrowser.Model.Services;
  9. using ServiceStack;
  10. namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
  11. {
  12. public partial class WebSocketSharpRequest : IHttpRequest
  13. {
  14. static internal string GetParameter(string header, string attr)
  15. {
  16. int ap = header.IndexOf(attr);
  17. if (ap == -1)
  18. return null;
  19. ap += attr.Length;
  20. if (ap >= header.Length)
  21. return null;
  22. char ending = header[ap];
  23. if (ending != '"')
  24. ending = ' ';
  25. int end = header.IndexOf(ending, ap + 1);
  26. if (end == -1)
  27. return ending == '"' ? null : header.Substring(ap);
  28. return header.Substring(ap + 1, end - ap - 1);
  29. }
  30. async Task LoadMultiPart()
  31. {
  32. string boundary = GetParameter(ContentType, "; boundary=");
  33. if (boundary == null)
  34. return;
  35. using (var requestStream = GetSubStream(InputStream, _memoryStreamProvider))
  36. {
  37. //DB: 30/01/11 - Hack to get around non-seekable stream and received HTTP request
  38. //Not ending with \r\n?
  39. var ms = _memoryStreamProvider.CreateNew(32 * 1024);
  40. await requestStream.CopyToAsync(ms).ConfigureAwait(false);
  41. var input = ms;
  42. ms.WriteByte((byte)'\r');
  43. ms.WriteByte((byte)'\n');
  44. input.Position = 0;
  45. //Uncomment to debug
  46. //var content = new StreamReader(ms).ReadToEnd();
  47. //Console.WriteLine(boundary + "::" + content);
  48. //input.Position = 0;
  49. var multi_part = new HttpMultipart(input, boundary, ContentEncoding);
  50. HttpMultipart.Element e;
  51. while ((e = multi_part.ReadNextElement()) != null)
  52. {
  53. if (e.Filename == null)
  54. {
  55. byte[] copy = new byte[e.Length];
  56. input.Position = e.Start;
  57. input.Read(copy, 0, (int)e.Length);
  58. form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy));
  59. }
  60. else
  61. {
  62. //
  63. // We use a substream, as in 2.x we will support large uploads streamed to disk,
  64. //
  65. HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length);
  66. files.AddFile(e.Name, sub);
  67. }
  68. }
  69. }
  70. }
  71. public QueryParamCollection Form
  72. {
  73. get
  74. {
  75. if (form == null)
  76. {
  77. form = new WebROCollection();
  78. files = new HttpFileCollection();
  79. if (IsContentType("multipart/form-data", true))
  80. {
  81. var task = LoadMultiPart();
  82. Task.WaitAll(task);
  83. }
  84. else if (IsContentType("application/x-www-form-urlencoded", true))
  85. {
  86. var task = LoadWwwForm();
  87. Task.WaitAll(task);
  88. }
  89. form.Protect();
  90. }
  91. #if NET_4_0
  92. if (validateRequestNewMode && !checked_form) {
  93. // Setting this before calling the validator prevents
  94. // possible endless recursion
  95. checked_form = true;
  96. ValidateNameValueCollection ("Form", query_string_nvc, RequestValidationSource.Form);
  97. } else
  98. #endif
  99. if (validate_form && !checked_form)
  100. {
  101. checked_form = true;
  102. ValidateNameValueCollection("Form", form);
  103. }
  104. return form;
  105. }
  106. }
  107. public string Accept
  108. {
  109. get
  110. {
  111. return string.IsNullOrEmpty(request.Headers[HttpHeaders.Accept]) ? null : request.Headers[HttpHeaders.Accept];
  112. }
  113. }
  114. public string Authorization
  115. {
  116. get
  117. {
  118. return string.IsNullOrEmpty(request.Headers[HttpHeaders.Authorization]) ? null : request.Headers[HttpHeaders.Authorization];
  119. }
  120. }
  121. protected bool validate_cookies, validate_query_string, validate_form;
  122. protected bool checked_cookies, checked_query_string, checked_form;
  123. static void ThrowValidationException(string name, string key, string value)
  124. {
  125. string v = "\"" + value + "\"";
  126. if (v.Length > 20)
  127. v = v.Substring(0, 16) + "...\"";
  128. string msg = String.Format("A potentially dangerous Request.{0} value was " +
  129. "detected from the client ({1}={2}).", name, key, v);
  130. throw new HttpRequestValidationException(msg);
  131. }
  132. static void ValidateNameValueCollection(string name, QueryParamCollection coll)
  133. {
  134. if (coll == null)
  135. return;
  136. foreach (var pair in coll)
  137. {
  138. var key = pair.Name;
  139. var val = pair.Value;
  140. if (val != null && val.Length > 0 && IsInvalidString(val))
  141. ThrowValidationException(name, key, val);
  142. }
  143. }
  144. internal static bool IsInvalidString(string val)
  145. {
  146. int validationFailureIndex;
  147. return IsInvalidString(val, out validationFailureIndex);
  148. }
  149. internal static bool IsInvalidString(string val, out int validationFailureIndex)
  150. {
  151. validationFailureIndex = 0;
  152. int len = val.Length;
  153. if (len < 2)
  154. return false;
  155. char current = val[0];
  156. for (int idx = 1; idx < len; idx++)
  157. {
  158. char next = val[idx];
  159. // See http://secunia.com/advisories/14325
  160. if (current == '<' || current == '\xff1c')
  161. {
  162. if (next == '!' || next < ' '
  163. || (next >= 'a' && next <= 'z')
  164. || (next >= 'A' && next <= 'Z'))
  165. {
  166. validationFailureIndex = idx - 1;
  167. return true;
  168. }
  169. }
  170. else if (current == '&' && next == '#')
  171. {
  172. validationFailureIndex = idx - 1;
  173. return true;
  174. }
  175. current = next;
  176. }
  177. return false;
  178. }
  179. public void ValidateInput()
  180. {
  181. validate_cookies = true;
  182. validate_query_string = true;
  183. validate_form = true;
  184. }
  185. bool IsContentType(string ct, bool starts_with)
  186. {
  187. if (ct == null || ContentType == null) return false;
  188. if (starts_with)
  189. return StrUtils.StartsWith(ContentType, ct, true);
  190. return String.Compare(ContentType, ct, true, Helpers.InvariantCulture) == 0;
  191. }
  192. async Task LoadWwwForm()
  193. {
  194. using (Stream input = GetSubStream(InputStream, _memoryStreamProvider))
  195. {
  196. using (var ms = _memoryStreamProvider.CreateNew())
  197. {
  198. await input.CopyToAsync(ms).ConfigureAwait(false);
  199. ms.Position = 0;
  200. using (StreamReader s = new StreamReader(ms, ContentEncoding))
  201. {
  202. StringBuilder key = new StringBuilder();
  203. StringBuilder value = new StringBuilder();
  204. int c;
  205. while ((c = s.Read()) != -1)
  206. {
  207. if (c == '=')
  208. {
  209. value.Length = 0;
  210. while ((c = s.Read()) != -1)
  211. {
  212. if (c == '&')
  213. {
  214. AddRawKeyValue(key, value);
  215. break;
  216. }
  217. else
  218. value.Append((char)c);
  219. }
  220. if (c == -1)
  221. {
  222. AddRawKeyValue(key, value);
  223. return;
  224. }
  225. }
  226. else if (c == '&')
  227. AddRawKeyValue(key, value);
  228. else
  229. key.Append((char)c);
  230. }
  231. if (c == -1)
  232. AddRawKeyValue(key, value);
  233. }
  234. }
  235. }
  236. }
  237. void AddRawKeyValue(StringBuilder key, StringBuilder value)
  238. {
  239. string decodedKey = HttpUtility.UrlDecode(key.ToString(), ContentEncoding);
  240. form.Add(decodedKey,
  241. HttpUtility.UrlDecode(value.ToString(), ContentEncoding));
  242. key.Length = 0;
  243. value.Length = 0;
  244. }
  245. WebROCollection form;
  246. HttpFileCollection files;
  247. public sealed class HttpFileCollection : NameObjectCollectionBase
  248. {
  249. internal HttpFileCollection()
  250. {
  251. }
  252. internal void AddFile(string name, HttpPostedFile file)
  253. {
  254. BaseAdd(name, file);
  255. }
  256. public void CopyTo(Array dest, int index)
  257. {
  258. /* XXX this is kind of gross and inefficient
  259. * since it makes a copy of the superclass's
  260. * list */
  261. object[] values = BaseGetAllValues();
  262. values.CopyTo(dest, index);
  263. }
  264. public string GetKey(int index)
  265. {
  266. return BaseGetKey(index);
  267. }
  268. public HttpPostedFile Get(int index)
  269. {
  270. return (HttpPostedFile)BaseGet(index);
  271. }
  272. public HttpPostedFile Get(string key)
  273. {
  274. return (HttpPostedFile)BaseGet(key);
  275. }
  276. public HttpPostedFile this[string key]
  277. {
  278. get
  279. {
  280. return Get(key);
  281. }
  282. }
  283. public HttpPostedFile this[int index]
  284. {
  285. get
  286. {
  287. return Get(index);
  288. }
  289. }
  290. public string[] AllKeys
  291. {
  292. get
  293. {
  294. return BaseGetAllKeys();
  295. }
  296. }
  297. }
  298. class WebROCollection : QueryParamCollection
  299. {
  300. bool got_id;
  301. int id;
  302. public bool GotID
  303. {
  304. get { return got_id; }
  305. }
  306. public int ID
  307. {
  308. get { return id; }
  309. set
  310. {
  311. got_id = true;
  312. id = value;
  313. }
  314. }
  315. public void Protect()
  316. {
  317. //IsReadOnly = true;
  318. }
  319. public void Unprotect()
  320. {
  321. //IsReadOnly = false;
  322. }
  323. public override string ToString()
  324. {
  325. StringBuilder result = new StringBuilder();
  326. foreach (var pair in this)
  327. {
  328. if (result.Length > 0)
  329. result.Append('&');
  330. var key = pair.Name;
  331. if (key != null && key.Length > 0)
  332. {
  333. result.Append(key);
  334. result.Append('=');
  335. }
  336. result.Append(pair.Value);
  337. }
  338. return result.ToString();
  339. }
  340. }
  341. public sealed class HttpPostedFile
  342. {
  343. string name;
  344. string content_type;
  345. Stream stream;
  346. class ReadSubStream : Stream
  347. {
  348. Stream s;
  349. long offset;
  350. long end;
  351. long position;
  352. public ReadSubStream(Stream s, long offset, long length)
  353. {
  354. this.s = s;
  355. this.offset = offset;
  356. this.end = offset + length;
  357. position = offset;
  358. }
  359. public override void Flush()
  360. {
  361. }
  362. public override int Read(byte[] buffer, int dest_offset, int count)
  363. {
  364. if (buffer == null)
  365. throw new ArgumentNullException("buffer");
  366. if (dest_offset < 0)
  367. throw new ArgumentOutOfRangeException("dest_offset", "< 0");
  368. if (count < 0)
  369. throw new ArgumentOutOfRangeException("count", "< 0");
  370. int len = buffer.Length;
  371. if (dest_offset > len)
  372. throw new ArgumentException("destination offset is beyond array size");
  373. // reordered to avoid possible integer overflow
  374. if (dest_offset > len - count)
  375. throw new ArgumentException("Reading would overrun buffer");
  376. if (count > end - position)
  377. count = (int)(end - position);
  378. if (count <= 0)
  379. return 0;
  380. s.Position = position;
  381. int result = s.Read(buffer, dest_offset, count);
  382. if (result > 0)
  383. position += result;
  384. else
  385. position = end;
  386. return result;
  387. }
  388. public override int ReadByte()
  389. {
  390. if (position >= end)
  391. return -1;
  392. s.Position = position;
  393. int result = s.ReadByte();
  394. if (result < 0)
  395. position = end;
  396. else
  397. position++;
  398. return result;
  399. }
  400. public override long Seek(long d, SeekOrigin origin)
  401. {
  402. long real;
  403. switch (origin)
  404. {
  405. case SeekOrigin.Begin:
  406. real = offset + d;
  407. break;
  408. case SeekOrigin.End:
  409. real = end + d;
  410. break;
  411. case SeekOrigin.Current:
  412. real = position + d;
  413. break;
  414. default:
  415. throw new ArgumentException();
  416. }
  417. long virt = real - offset;
  418. if (virt < 0 || virt > Length)
  419. throw new ArgumentException();
  420. position = s.Seek(real, SeekOrigin.Begin);
  421. return position;
  422. }
  423. public override void SetLength(long value)
  424. {
  425. throw new NotSupportedException();
  426. }
  427. public override void Write(byte[] buffer, int offset, int count)
  428. {
  429. throw new NotSupportedException();
  430. }
  431. public override bool CanRead
  432. {
  433. get { return true; }
  434. }
  435. public override bool CanSeek
  436. {
  437. get { return true; }
  438. }
  439. public override bool CanWrite
  440. {
  441. get { return false; }
  442. }
  443. public override long Length
  444. {
  445. get { return end - offset; }
  446. }
  447. public override long Position
  448. {
  449. get
  450. {
  451. return position - offset;
  452. }
  453. set
  454. {
  455. if (value > Length)
  456. throw new ArgumentOutOfRangeException();
  457. position = Seek(value, SeekOrigin.Begin);
  458. }
  459. }
  460. }
  461. internal HttpPostedFile(string name, string content_type, Stream base_stream, long offset, long length)
  462. {
  463. this.name = name;
  464. this.content_type = content_type;
  465. this.stream = new ReadSubStream(base_stream, offset, length);
  466. }
  467. public string ContentType
  468. {
  469. get
  470. {
  471. return content_type;
  472. }
  473. }
  474. public int ContentLength
  475. {
  476. get
  477. {
  478. return (int)stream.Length;
  479. }
  480. }
  481. public string FileName
  482. {
  483. get
  484. {
  485. return name;
  486. }
  487. }
  488. public Stream InputStream
  489. {
  490. get
  491. {
  492. return stream;
  493. }
  494. }
  495. }
  496. class Helpers
  497. {
  498. public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;
  499. }
  500. internal sealed class StrUtils
  501. {
  502. StrUtils() { }
  503. public static bool StartsWith(string str1, string str2)
  504. {
  505. return StartsWith(str1, str2, false);
  506. }
  507. public static bool StartsWith(string str1, string str2, bool ignore_case)
  508. {
  509. int l2 = str2.Length;
  510. if (l2 == 0)
  511. return true;
  512. int l1 = str1.Length;
  513. if (l2 > l1)
  514. return false;
  515. return 0 == String.Compare(str1, 0, str2, 0, l2, ignore_case, Helpers.InvariantCulture);
  516. }
  517. public static bool EndsWith(string str1, string str2)
  518. {
  519. return EndsWith(str1, str2, false);
  520. }
  521. public static bool EndsWith(string str1, string str2, bool ignore_case)
  522. {
  523. int l2 = str2.Length;
  524. if (l2 == 0)
  525. return true;
  526. int l1 = str1.Length;
  527. if (l2 > l1)
  528. return false;
  529. return 0 == String.Compare(str1, l1 - l2, str2, 0, l2, ignore_case, Helpers.InvariantCulture);
  530. }
  531. }
  532. class HttpMultipart
  533. {
  534. public class Element
  535. {
  536. public string ContentType;
  537. public string Name;
  538. public string Filename;
  539. public Encoding Encoding;
  540. public long Start;
  541. public long Length;
  542. public override string ToString()
  543. {
  544. return "ContentType " + ContentType + ", Name " + Name + ", Filename " + Filename + ", Start " +
  545. Start.ToString() + ", Length " + Length.ToString();
  546. }
  547. }
  548. Stream data;
  549. string boundary;
  550. byte[] boundary_bytes;
  551. byte[] buffer;
  552. bool at_eof;
  553. Encoding encoding;
  554. StringBuilder sb;
  555. const byte HYPHEN = (byte)'-', LF = (byte)'\n', CR = (byte)'\r';
  556. // See RFC 2046
  557. // In the case of multipart entities, in which one or more different
  558. // sets of data are combined in a single body, a "multipart" media type
  559. // field must appear in the entity's header. The body must then contain
  560. // one or more body parts, each preceded by a boundary delimiter line,
  561. // and the last one followed by a closing boundary delimiter line.
  562. // After its boundary delimiter line, each body part then consists of a
  563. // header area, a blank line, and a body area. Thus a body part is
  564. // similar to an RFC 822 message in syntax, but different in meaning.
  565. public HttpMultipart(Stream data, string b, Encoding encoding)
  566. {
  567. this.data = data;
  568. //DB: 30/01/11: cannot set or read the Position in HttpListener in Win.NET
  569. //var ms = new MemoryStream(32 * 1024);
  570. //data.CopyTo(ms);
  571. //this.data = ms;
  572. boundary = b;
  573. boundary_bytes = encoding.GetBytes(b);
  574. buffer = new byte[boundary_bytes.Length + 2]; // CRLF or '--'
  575. this.encoding = encoding;
  576. sb = new StringBuilder();
  577. }
  578. string ReadLine()
  579. {
  580. // CRLF or LF are ok as line endings.
  581. bool got_cr = false;
  582. int b = 0;
  583. sb.Length = 0;
  584. while (true)
  585. {
  586. b = data.ReadByte();
  587. if (b == -1)
  588. {
  589. return null;
  590. }
  591. if (b == LF)
  592. {
  593. break;
  594. }
  595. got_cr = b == CR;
  596. sb.Append((char)b);
  597. }
  598. if (got_cr)
  599. sb.Length--;
  600. return sb.ToString();
  601. }
  602. static string GetContentDispositionAttribute(string l, string name)
  603. {
  604. int idx = l.IndexOf(name + "=\"");
  605. if (idx < 0)
  606. return null;
  607. int begin = idx + name.Length + "=\"".Length;
  608. int end = l.IndexOf('"', begin);
  609. if (end < 0)
  610. return null;
  611. if (begin == end)
  612. return "";
  613. return l.Substring(begin, end - begin);
  614. }
  615. string GetContentDispositionAttributeWithEncoding(string l, string name)
  616. {
  617. int idx = l.IndexOf(name + "=\"");
  618. if (idx < 0)
  619. return null;
  620. int begin = idx + name.Length + "=\"".Length;
  621. int end = l.IndexOf('"', begin);
  622. if (end < 0)
  623. return null;
  624. if (begin == end)
  625. return "";
  626. string temp = l.Substring(begin, end - begin);
  627. byte[] source = new byte[temp.Length];
  628. for (int i = temp.Length - 1; i >= 0; i--)
  629. source[i] = (byte)temp[i];
  630. return encoding.GetString(source);
  631. }
  632. bool ReadBoundary()
  633. {
  634. try
  635. {
  636. string line = ReadLine();
  637. while (line == "")
  638. line = ReadLine();
  639. if (line[0] != '-' || line[1] != '-')
  640. return false;
  641. if (!StrUtils.EndsWith(line, boundary, false))
  642. return true;
  643. }
  644. catch
  645. {
  646. }
  647. return false;
  648. }
  649. string ReadHeaders()
  650. {
  651. string s = ReadLine();
  652. if (s == "")
  653. return null;
  654. return s;
  655. }
  656. bool CompareBytes(byte[] orig, byte[] other)
  657. {
  658. for (int i = orig.Length - 1; i >= 0; i--)
  659. if (orig[i] != other[i])
  660. return false;
  661. return true;
  662. }
  663. long MoveToNextBoundary()
  664. {
  665. long retval = 0;
  666. bool got_cr = false;
  667. int state = 0;
  668. int c = data.ReadByte();
  669. while (true)
  670. {
  671. if (c == -1)
  672. return -1;
  673. if (state == 0 && c == LF)
  674. {
  675. retval = data.Position - 1;
  676. if (got_cr)
  677. retval--;
  678. state = 1;
  679. c = data.ReadByte();
  680. }
  681. else if (state == 0)
  682. {
  683. got_cr = c == CR;
  684. c = data.ReadByte();
  685. }
  686. else if (state == 1 && c == '-')
  687. {
  688. c = data.ReadByte();
  689. if (c == -1)
  690. return -1;
  691. if (c != '-')
  692. {
  693. state = 0;
  694. got_cr = false;
  695. continue; // no ReadByte() here
  696. }
  697. int nread = data.Read(buffer, 0, buffer.Length);
  698. int bl = buffer.Length;
  699. if (nread != bl)
  700. return -1;
  701. if (!CompareBytes(boundary_bytes, buffer))
  702. {
  703. state = 0;
  704. data.Position = retval + 2;
  705. if (got_cr)
  706. {
  707. data.Position++;
  708. got_cr = false;
  709. }
  710. c = data.ReadByte();
  711. continue;
  712. }
  713. if (buffer[bl - 2] == '-' && buffer[bl - 1] == '-')
  714. {
  715. at_eof = true;
  716. }
  717. else if (buffer[bl - 2] != CR || buffer[bl - 1] != LF)
  718. {
  719. state = 0;
  720. data.Position = retval + 2;
  721. if (got_cr)
  722. {
  723. data.Position++;
  724. got_cr = false;
  725. }
  726. c = data.ReadByte();
  727. continue;
  728. }
  729. data.Position = retval + 2;
  730. if (got_cr)
  731. data.Position++;
  732. break;
  733. }
  734. else
  735. {
  736. // state == 1
  737. state = 0; // no ReadByte() here
  738. }
  739. }
  740. return retval;
  741. }
  742. public Element ReadNextElement()
  743. {
  744. if (at_eof || ReadBoundary())
  745. return null;
  746. Element elem = new Element();
  747. string header;
  748. while ((header = ReadHeaders()) != null)
  749. {
  750. if (StrUtils.StartsWith(header, "Content-Disposition:", true))
  751. {
  752. elem.Name = GetContentDispositionAttribute(header, "name");
  753. elem.Filename = StripPath(GetContentDispositionAttributeWithEncoding(header, "filename"));
  754. }
  755. else if (StrUtils.StartsWith(header, "Content-Type:", true))
  756. {
  757. elem.ContentType = header.Substring("Content-Type:".Length).Trim();
  758. elem.Encoding = GetEncoding(elem.ContentType);
  759. }
  760. }
  761. long start = 0;
  762. start = data.Position;
  763. elem.Start = start;
  764. long pos = MoveToNextBoundary();
  765. if (pos == -1)
  766. return null;
  767. elem.Length = pos - start;
  768. return elem;
  769. }
  770. static string StripPath(string path)
  771. {
  772. if (path == null || path.Length == 0)
  773. return path;
  774. if (path.IndexOf(":\\") != 1 && !path.StartsWith("\\\\"))
  775. return path;
  776. return path.Substring(path.LastIndexOf('\\') + 1);
  777. }
  778. }
  779. }
  780. }