Exemplo n.º 1
0
        private HttpServerRequest ParseRawRequest(string rawrequest)
        {
            var request = new HttpServerRequest();

            string[] requestParts = rawrequest.ToString().Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);

            string rawHeader = requestParts[0];

            request.Body = requestParts[1];

            string[] headerParts        = rawHeader.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.None);
            string[] httpStartLineParts = headerParts[0].Split(' ');

            request.Method = httpStartLineParts[0];
            request.Path   = httpStartLineParts[1];

            request.Headers = new Dictionary <string, string>();
            for (int i = 1; i < headerParts.Length; i++)
            {
                string[] header = headerParts[i].Split(':');

                request.Headers.Add(header[0].Trim(), header[1].Trim());
            }

            return(request);
        }
Exemplo n.º 2
0
        private async Task WriteResponseAsync(HttpServerRequest request, IOutputStream os)
        {
            using (Stream response = os.AsStreamForWrite())
            {
                bool exists = true;
                try
                {
                    if (ServerResponseCallback != null)
                    {
                        byte[] bytes = ServerResponseCallback(request);
                        await response.WriteAsync(bytes, 0, bytes.Length);
                    }
                    else if (ServerResponseCallbackAsync != null)
                    {
                        byte[] bytes = await ServerResponseCallbackAsync(request);

                        System.Diagnostics.Debug.WriteLine(System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length));

                        await response.WriteAsync(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        // Look in the Data subdirectory of the app package
                        string filePath = "Data" + request.Path.Replace('/', '\\');
                        using (Stream fs = await LocalFolder.OpenStreamForReadAsync(filePath))
                        {
                            string header = String.Format("HTTP/1.1 200 OK\r\n" +
                                                          "Content-Length: {0}\r\n" +
                                                          "Connection: close\r\n\r\n",
                                                          fs.Length);
                            byte[] headerArray = Encoding.UTF8.GetBytes(header);
                            await response.WriteAsync(headerArray, 0, headerArray.Length);

                            await fs.CopyToAsync(response);
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    exists = false;
                }

                if (!exists)
                {
                    byte[] headerArray = Encoding.UTF8.GetBytes(
                        "HTTP/1.1 404 Not Found\r\n" +
                        "Content-Length:0\r\n" +
                        "Connection: close\r\n\r\n");
                    await response.WriteAsync(headerArray, 0, headerArray.Length);
                }

                await response.FlushAsync();
            }
        }
Exemplo n.º 3
0
        private async static Task<byte[]> UsernamePasswordEchoHandler(HttpServerRequest request)
        {
            var response = await Handlers.LoadResponse(request.Path);

            var authHeader = request.Headers["Authorization"];
            byte[] base64 = Convert.FromBase64String(authHeader.Substring("Basic ".Length));
            var parts = Encoding.UTF8.GetString(base64, 0, base64.Length).Split(':');

            byte[] bodyArray = Encoding.UTF8.GetBytes(string.Join("|", parts));

            string intro = String.Format("HTTP/1.1 {0} OK\r\n", response.StatusCode);

            string headers = Handlers.BuildHeaders(response.Headers);
            headers = headers + string.Format("Content-Length: {0}\r\n\r\n", bodyArray.Length);

            byte[] headerArray = Encoding.UTF8.GetBytes(intro + headers);

            byte[] result = headerArray.Concat(bodyArray).ToArray();
            return result;
        }
Exemplo n.º 4
0
        private HttpServerRequest ParseRawRequest(string rawrequest)
        {
            var request = new HttpServerRequest();
            
            string[] requestParts = rawrequest.ToString().Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);

            string rawHeader = requestParts[0];
            request.Body = requestParts[1];

            string[] headerParts = rawHeader.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.None);
            string[] httpStartLineParts = headerParts[0].Split(' ');

            request.Method = httpStartLineParts[0];
            request.Path = httpStartLineParts[1];
            
            request.Headers = new Dictionary<string, string>();
            for (int i = 1; i < headerParts.Length; i++)
            {
                string[] header = headerParts[i].Split(':');

                request.Headers.Add(header[0].Trim(), header[1].Trim());
            }

            return request;
        }
Exemplo n.º 5
0
        private async Task WriteResponseAsync(HttpServerRequest request, IOutputStream os)
        {
            using (Stream response = os.AsStreamForWrite())
            {
                bool exists = true;
                try
                {
                    if (ServerResponseCallback != null)
                    {
                        byte[] bytes = ServerResponseCallback(request);
                        await response.WriteAsync(bytes, 0, bytes.Length);
                    }
                    else if (ServerResponseCallbackAsync != null)
                    {
                        byte[] bytes = await ServerResponseCallbackAsync(request);

                        System.Diagnostics.Debug.WriteLine(System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length));

                        await response.WriteAsync(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        // Look in the Data subdirectory of the app package
                        string filePath = "Data" + request.Path.Replace('/', '\\');
                        using (Stream fs = await LocalFolder.OpenStreamForReadAsync(filePath))
                        {
                            string header = String.Format("HTTP/1.1 200 OK\r\n" +
                                             "Content-Length: {0}\r\n" +
                                             "Connection: close\r\n\r\n",
                                             fs.Length);
                            byte[] headerArray = Encoding.UTF8.GetBytes(header);
                            await response.WriteAsync(headerArray, 0, headerArray.Length);
                            await fs.CopyToAsync(response);
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    exists = false;
                }

                if (!exists)
                {
                    byte[] headerArray = Encoding.UTF8.GetBytes(
                                          "HTTP/1.1 404 Not Found\r\n" +
                                          "Content-Length:0\r\n" +
                                          "Connection: close\r\n\r\n");
                    await response.WriteAsync(headerArray, 0, headerArray.Length);
                }

                await response.FlushAsync();
            }
        }