|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Nancy; |
| 6 | +using Kayak.Http; |
| 7 | +using Nancy.Helpers; |
| 8 | +using System.IO; |
| 9 | + |
| 10 | +namespace CloudlogCAT.HttpServer |
| 11 | +{ |
| 12 | + internal sealed class NancyRequestDelegate : IHttpRequestDelegate |
| 13 | + { |
| 14 | + private INancyEngine m_Engine; |
| 15 | + |
| 16 | + public NancyRequestDelegate(INancyEngine engine) |
| 17 | + { |
| 18 | + m_Engine = engine; |
| 19 | + } |
| 20 | + |
| 21 | + public void OnRequest(HttpRequestHead head, Kayak.IDataProducer body, IHttpResponseDelegate response) |
| 22 | + { |
| 23 | + BufferedConsumer requestConsumer = new BufferedConsumer(bodyStream => RequestReadCompleted(head, bodyStream, response), RequestReadError); |
| 24 | + body.Connect(requestConsumer); |
| 25 | + } |
| 26 | + |
| 27 | + private void RequestReadCompleted(HttpRequestHead head, Stream requestStream, IHttpResponseDelegate response) |
| 28 | + { |
| 29 | +#warning wrong hard-coded base URI |
| 30 | + Uri baseUri = new Uri("http://localhost:8083/"); |
| 31 | + Uri requestUrl = new Uri(baseUri, head.Uri); |
| 32 | + Url nancyUrl = new Url |
| 33 | + { |
| 34 | + Scheme = requestUrl.Scheme, |
| 35 | + HostName = requestUrl.Host, |
| 36 | + Port = requestUrl.IsDefaultPort ? null : (int?)requestUrl.Port, |
| 37 | + BasePath = baseUri.AbsolutePath.TrimEnd('/'), |
| 38 | + Path = HttpUtility.UrlDecode(head.Uri), |
| 39 | + Query = requestUrl.Query, |
| 40 | + Fragment = requestUrl.Fragment, |
| 41 | + }; |
| 42 | + Dictionary<string, IEnumerable<string>> headers = new Dictionary<string, IEnumerable<string>>(); |
| 43 | + foreach (var kvp in head.Headers) |
| 44 | + headers[kvp.Key] = new List<string> { kvp.Value }; |
| 45 | + |
| 46 | + Request req = new Request( |
| 47 | + head.Method, |
| 48 | + nancyUrl, |
| 49 | + Nancy.IO.RequestStream.FromStream(requestStream, requestStream.Length), |
| 50 | + headers, |
| 51 | + null); |
| 52 | + |
| 53 | + m_Engine.HandleRequest(req, context => RequestProcessingCompleted(context, response), ex => RequestProcessingError(ex, response)); |
| 54 | + } |
| 55 | + |
| 56 | + private void RequestReadError(Exception ex) |
| 57 | + { |
| 58 | + throw new NotImplementedException(); |
| 59 | + } |
| 60 | + |
| 61 | + private void RequestProcessingCompleted(NancyContext context, IHttpResponseDelegate response) |
| 62 | + { |
| 63 | + HttpResponseHead responseHead = new HttpResponseHead { |
| 64 | + Headers = context.Response.Headers, |
| 65 | + Status = context.Response.StatusCode.ToString() |
| 66 | + }; |
| 67 | + |
| 68 | + byte[] responseBodyData; |
| 69 | + using (MemoryStream ms = new MemoryStream()) |
| 70 | + { |
| 71 | + context.Response.Contents(ms); |
| 72 | + //ms.Seek(0, SeekOrigin.Begin); |
| 73 | + responseBodyData = ms.ToArray(); |
| 74 | + } |
| 75 | + |
| 76 | + responseHead.Headers["Content-Type"] = context.Response.ContentType; |
| 77 | + responseHead.Headers["Content-Length"] = responseBodyData.LongLength.ToString(); |
| 78 | + |
| 79 | + BufferedProducer bodyDataProducer = new BufferedProducer (responseBodyData); |
| 80 | + response.OnResponse(responseHead, bodyDataProducer); |
| 81 | + } |
| 82 | + |
| 83 | + private void RequestProcessingError(Exception ex, IHttpResponseDelegate response) |
| 84 | + { |
| 85 | + throw new NotImplementedException(); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments