Example #1
0
        public HttpResponse
        (
            HttpResponseCode code,
            string contentType,
            Stream contentStream,
            bool keepAliveConnection,
            IEnumerable <KeyValuePair <string, string> > headers
        )
        {
            ContentStream = contentStream;

            CloseConnection = !keepAliveConnection;

            ResponseCode = code;
            Headers      = new ListHttpHeaders
                           (
                new[]
            {
                new KeyValuePair <string, string>("Date", DateTime.UtcNow.ToString("R")),
                new KeyValuePair <string, string>("Connection", CloseConnection ? "Close" : "Keep-Alive"),
                new KeyValuePair <string, string>("Content-Type", contentType),
                new KeyValuePair <string, string>
                    ("Content-Length", ContentStream.Length.ToString(CultureInfo.InvariantCulture))
            }.Concat(headers)
                .ToList()
                           );
        }
        public void Start()
        {
            try
            {
                server          = new HttpServer(provider);
                tcpListener     = new TcpListener(IPAddress.Any, 11000);
                tcpListenerAdpt = new TcpListenerAdapter(tcpListener);

                server.Use(tcpListenerAdpt);
                server.Use((context, next) =>
                {
                    Debug.WriteLine("Receiver a HTTP request!================================");

                    var request_method   = context.Request.Method.ToString();
                    var request_protocol = context.Request.Protocol;
                    var request_uri      = context.Request.Uri.ToString();
                    var request_content  = Encoding.UTF8.GetString(context.Request.Post.Raw);
                    var headers          = context.Request.Headers;

                    Debug.WriteLine("Method: " + request_method);
                    Debug.WriteLine("Protocol: " + request_protocol);
                    Debug.WriteLine("Uri: " + request_uri);
                    Debug.WriteLine("Body: " + request_content);

                    if (request_method.ToUpper() == "POST")
                    {
                        var obj = new NotificationObject(request_content);

                        Debug.WriteLine("Path[" + obj.Path + "] receive data: " + obj.Content);

                        var resource_path = obj.Path;
                        var resource_name = obj.ResourceName;
                        var msg_id        = context.Request.Headers.GetByName("X-M2M-RI");

                        //call handler

                        var list_headers = new List <KeyValuePair <string, string> >();

                        list_headers.Add(new KeyValuePair <string, string>("X-M2M-RSC", "2001"));
                        list_headers.Add(new KeyValuePair <string, string>("X-M2M-RI", msg_id));

                        var response_headers = new ListHttpHeaders(list_headers);

                        context.Response = uhttpsharp.StringHttpResponse.Create("", HttpResponseCode.Created, "text/html", true, response_headers);
                    }

                    return(Task.Factory.GetCompleted());
                });

                server.Start();

                IsActived = true;
            }
            catch (Exception exp)
            {
                IsActived = false;
                throw exp;
            }
        }
Example #3
0
        protected HttpResponse XMLResponse(IHttpContext context, FileStream stream)
        {
            var headers = new ListHttpHeaders(new[] {
                new KeyValuePair <string, string>("Cache-Control", "no-store")
            });

            return(new HttpResponse(HttpResponseCode.Ok, "application/xml; charset-utf-8", stream, false, headers));
        }
Example #4
0
        protected HttpResponse StreamResponse(IHttpContext context, string contentType, Stream stream)
        {
            var headers = new ListHttpHeaders(new[] {
                new KeyValuePair <string, string>("Cache-Control", "no-store")
            });

            return(new HttpResponse(HttpResponseCode.Ok, contentType, stream, false, headers));
        }
Example #5
0
        protected IHttpResponse Found(string url)
        {
            var headers = new ListHttpHeaders(new[] {
                new KeyValuePair <string, string>("Location", url)
            });

            return(StringHttpResponse.Create(string.Empty, HttpResponseCode.Found, null, false, headers));
        }
 public BasicAuthenticationHandler(string realm, string username, string password)
 {
     _username          = username;
     _password          = password;
     _authenticationKey = "Authenticated." + realm;
     _headers           = new ListHttpHeaders(new List <KeyValuePair <string, string> >
     {
         new KeyValuePair <string, string>("WWW-Authenticate", string.Format(@"Basic realm=""{0}""", realm))
     });
 }
Example #7
0
        public CompressedResponse(IHttpResponse child, MemoryStream memoryStream, string encoding)
        {
            _memoryStream = memoryStream;

            ResponseCode    = child.ResponseCode;
            CloseConnection = child.CloseConnection;
            Headers         =
                new ListHttpHeaders(
                    child.Headers.Where(h => !h.Key.Equals("content-length", StringComparison.InvariantCultureIgnoreCase))
                    .Concat(new[] {
                new KeyValuePair <string, string>("content-length", memoryStream.Length.ToString(CultureInfo.InvariantCulture)),
                new KeyValuePair <string, string>("content-encoding", encoding)
            })
                    .ToList());
        }
Example #8
0
        protected HttpResponse JsonResponse(IHttpContext context, object value)
        {
            var headers = new ListHttpHeaders(new[] {
                new KeyValuePair <string, string>("Cache-Control", "no-store")
            });

            var serializer   = new JsonSerializer();
            var stream       = new MemoryStream();
            var streamWriter = new StreamWriter(stream);

            serializer.Serialize(streamWriter, value);

            streamWriter.Flush();
            stream.Position = 0;

            return(new HttpResponse(HttpResponseCode.Ok, "application/json; charset-utf-8", stream, false, headers));
        }