예제 #1
0
        public void HttpRequestChanged(object sender, EventArgs args)
        {
            HttpRequestEventArgs httpArgs = (HttpRequestEventArgs)args;

            _httpDetails = httpArgs.Details;
            string body = (string)httpArgs.Body;

            if (_httpDetails.HttpPath == "audio")
            {
                _requestDictionary = _serializer.Deserialize <Dictionary <string, object> >(body);
                _actions[(string)_requestDictionary["action"]]();
            }
        }
예제 #2
0
        public void Process(HttpListenerContext context, string baseFolderPath, string[] httpMethods)
        {
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;
            string httpMethod             = request.HttpMethod.ToLower();
            string httpPath    = request.RawUrl.Split('?')[0];
            string foundMethod = Array.Find(httpMethods, method => method == httpMethod);

            if (foundMethod != null)
            {
                if (httpPath == "/")
                {
                    string indexPath = Path.Combine(baseFolderPath, "index.html");
                    if (File.Exists(indexPath))
                    {
                        string bufferStr = File.ReadAllText(indexPath);
                        byte[] buffer    = Encoding.UTF8.GetBytes(bufferStr);
                        response.ContentLength64 = buffer.Length;
                        Stream output = response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);
                        output.Close();
                        response.Close();
                    }
                }
                else
                {
                    httpPath = httpPath.Substring(1).Replace("/", @"\");
                    string filePath    = Path.Combine(baseFolderPath, httpPath);
                    string contentType = "";
                    if (File.Exists(filePath))
                    {
                        string ext = Path.GetExtension(filePath).TrimStart(".".ToCharArray());
                        if (_helpers.MimeTypes.ContainsKey(ext))
                        {
                            contentType = _helpers.MimeTypes[ext];
                        }
                        else
                        {
                            contentType = GetContentType(ext);
                        }
                        byte[] buffer = File.ReadAllBytes(filePath);
                        response.ContentLength64 = buffer.Length;
                        response.ContentType     = contentType;
                        Stream output = response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);
                        output.Close();
                        response.Close();
                    }
                    else
                    {
                        HttpConnectionDetails details = new HttpConnectionDetails(
                            response,
                            httpPath,
                            httpMethod,
                            request.ContentLength64,
                            request.ContentType);
                        StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
                        string       body   = reader.ReadToEnd();
                        request.InputStream.Close();
                        reader.Close();
                        HttpRequestEventArgs args = new HttpRequestEventArgs(body, details);
                        _onHttpRequestChanged(this, args);
                    }
                }
            }
        }
 public HttpRequestEventArgs(object body, HttpConnectionDetails details)
 {
     Body    = body;
     Details = details;
 }