Mercurial > hg > ucis.core
comparison Net/HTTP.cs @ 95:ebdff34b9e4f
Merge
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Thu, 26 Jun 2014 18:45:56 +0200 |
parents | 146a8d224d86 3c1bba376dca |
children | be17dd3f6927 2b5e7bb9b979 |
comparison
equal
deleted
inserted
replaced
87:9b898d8b2541 | 95:ebdff34b9e4f |
---|---|
94 | 94 |
95 private StreamWriter Writer; | 95 private StreamWriter Writer; |
96 private PrebufferingStream Reader; | 96 private PrebufferingStream Reader; |
97 private List<HTTPHeader> RequestHeaders; | 97 private List<HTTPHeader> RequestHeaders; |
98 private HTTPConnectionState State = HTTPConnectionState.Starting; | 98 private HTTPConnectionState State = HTTPConnectionState.Starting; |
99 private KeyValuePair<String, String>[] QueryParameters = null, PostParameters = null; | 99 private KeyValuePair<String, String>[] QueryParameters = null, PostParameters = null, Cookies = null; |
100 private HTTPOutputStream ResponseStream = null; | 100 private HTTPOutputStream ResponseStream = null; |
101 private HTTPInputStream RequestStream = null; | 101 private HTTPInputStream RequestStream = null; |
102 | 102 |
103 private enum HTTPConnectionState { | 103 private enum HTTPConnectionState { |
104 Starting = 0, | 104 Starting = 0, |
537 List<String> list = new List<string>(); | 537 List<String> list = new List<string>(); |
538 foreach (KeyValuePair<String, String> kvp in GetQueryParameters()) if (kvp.Key == name) list.Add(kvp.Value); | 538 foreach (KeyValuePair<String, String> kvp in GetQueryParameters()) if (kvp.Key == name) list.Add(kvp.Value); |
539 return list.ToArray(); | 539 return list.ToArray(); |
540 } | 540 } |
541 public KeyValuePair<String, String>[] GetQueryParameters() { | 541 public KeyValuePair<String, String>[] GetQueryParameters() { |
542 if (RequestQuery == null) return new KeyValuePair<String, String>[0]; | |
542 if (QueryParameters == null) QueryParameters = DecodeUrlEncodedFields(RequestQuery); | 543 if (QueryParameters == null) QueryParameters = DecodeUrlEncodedFields(RequestQuery); |
543 return QueryParameters; | 544 return QueryParameters; |
544 } | 545 } |
545 | 546 |
546 public String GetPostParameter(String name) { | 547 public String GetPostParameter(String name) { |
561 } else { | 562 } else { |
562 PostParameters = new KeyValuePair<string, string>[0]; | 563 PostParameters = new KeyValuePair<string, string>[0]; |
563 } | 564 } |
564 } | 565 } |
565 return PostParameters; | 566 return PostParameters; |
567 } | |
568 | |
569 public String GetCookie(String name) { | |
570 foreach (KeyValuePair<String, String> kvp in GetCookies()) if (kvp.Key == name) return kvp.Value; | |
571 return null; | |
572 } | |
573 public String[] GetCookies(String name) { | |
574 List<String> list = new List<string>(); | |
575 foreach (KeyValuePair<String, String> kvp in GetCookies()) if (kvp.Key == name) list.Add(kvp.Value); | |
576 return list.ToArray(); | |
577 } | |
578 public KeyValuePair<String, String>[] GetCookies() { | |
579 if (Cookies == null) { | |
580 String cookie = GetRequestHeader("Cookie"); | |
581 List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); | |
582 if (cookie != null) { | |
583 foreach (String part in cookie.Split(';', ',')) { | |
584 String[] subparts = part.Split('='); | |
585 String key = subparts[0].Trim(' ', '\t', '"'); | |
586 String value = (subparts.Length < 2) ? null : subparts[1].Trim(' ', '\t', '"'); | |
587 list.Add(new KeyValuePair<string, string>(key, value)); | |
588 } | |
589 } | |
590 Cookies = list.ToArray(); | |
591 } | |
592 return Cookies; | |
593 } | |
594 | |
595 public void SetCookie(String name, String value) { | |
596 SendHeader("Set-Cookie", String.Format("{0}={1}", name, value)); | |
597 } | |
598 public void SetCookie(String name, String value, DateTime expire) { | |
599 SendHeader("Set-Cookie", String.Format("{0}={1}; Expires={2:R}", name, value, expire)); | |
600 } | |
601 public void SetCookie(String name, String value, DateTime? expire, String path, String domain, Boolean secure, Boolean httponly) { | |
602 StringBuilder sb = new StringBuilder(); | |
603 sb.Append(name); | |
604 sb.Append("="); | |
605 sb.Append(value); | |
606 if (expire != null) sb.AppendFormat("; Expires={0:R}", expire.Value.ToUniversalTime()); | |
607 if (path != null) sb.AppendFormat("; Path={0}", path); | |
608 if (domain != null) sb.AppendFormat("; Domain={0}", domain); | |
609 if (secure) sb.Append("; Secure"); | |
610 if (httponly) sb.Append("; HttpOnly"); | |
611 SendHeader("Set-Cookie", sb.ToString()); | |
566 } | 612 } |
567 | 613 |
568 public Stream OpenRequestStream() { | 614 public Stream OpenRequestStream() { |
569 if (RequestStream == null) RequestStream = new HTTPInputStream(this); | 615 if (RequestStream == null) RequestStream = new HTTPInputStream(this); |
570 return RequestStream; | 616 return RequestStream; |
706 Prefixes = new List<KeyValuePair<string, IHTTPContentProvider>>(); | 752 Prefixes = new List<KeyValuePair<string, IHTTPContentProvider>>(); |
707 PrefixComparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; | 753 PrefixComparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; |
708 } | 754 } |
709 public void AddPrefix(String prefix, IHTTPContentProvider contentProvider) { | 755 public void AddPrefix(String prefix, IHTTPContentProvider contentProvider) { |
710 Prefixes.Add(new KeyValuePair<string, IHTTPContentProvider>(prefix, contentProvider)); | 756 Prefixes.Add(new KeyValuePair<string, IHTTPContentProvider>(prefix, contentProvider)); |
757 Prefixes.Sort(delegate(KeyValuePair<String, IHTTPContentProvider> a, KeyValuePair<String, IHTTPContentProvider> b) { | |
758 return -String.CompareOrdinal(a.Key, b.Key); | |
759 }); | |
711 } | 760 } |
712 public void DeletePrefix(String prefix) { | 761 public void DeletePrefix(String prefix) { |
713 Prefixes.RemoveAll(delegate(KeyValuePair<string, IHTTPContentProvider> item) { return prefix.Equals(item.Key, PrefixComparison); }); | 762 Prefixes.RemoveAll(delegate(KeyValuePair<string, IHTTPContentProvider> item) { return prefix.Equals(item.Key, PrefixComparison); }); |
714 } | 763 } |
715 public void ServeRequest(HTTPContext context) { | 764 public void ServeRequest(HTTPContext context) { |