RequestMono.cs 28 KB

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