RequestMono.cs 28 KB

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