Пример #1
0
        protected async Task SendHTTP(int conId, HTTPReply reply, Stream response, string hostname)
        {
            byte[] content = (byte[])reply.Response;
            await response.WriteAsync(content, 0, content.Length).ConfigureAwait(false);

            await response.FlushAsync().ConfigureAwait(false);

            Debug.WriteLine("{" + conId + "} Response " + Encoding.UTF8.GetString((byte[])reply.Response).Split("\n")[0] + " - " + hostname);
        }
Пример #2
0
        protected async Task _doRequest(int conId, string hostname, IInputStream input, Stream response)
        {
            bool persistent      = true;
            byte persistentCount = 99;

            while (persistent)
            {
                HTTPReply reply = new HTTPReply(HttpVersion.Http11);
                try
                {
                    HTTPRequest request = await ReadHTTP(conId, input, hostname);

                    Debug.WriteLine("{" + conId + "} Read : " + request.Method + " " + request.URLPath + " " + request.Version + " - " + hostname);

                    if (!request.Persistent || persistentCount-- < 1)
                    {
                        persistent = false;
                    }

                    reply = new HTTPReply(request.Version)
                    {
                        IsHead       = (request.Method == HttpMethod.Head),
                        IsPersistent = persistent
                    };

                    string finalPath = getFinalPath(request.URLPath);

                    bool      GetExists      = GetPaths.TryGetValue(finalPath, out Func <HTTPRequest, JSONReply> GetEntry);
                    bool      PostExists     = PostPaths.TryGetValue(finalPath, out Func <HTTPRequest, JSONReply> PostEntry);
                    JSONReply GetEntryReply  = GetExists ? GetEntry(request) : null;
                    JSONReply PostEntryReply = PostExists ? PostEntry(request) : null;

                    HandleMethod(ref request, ref reply, ref GetEntryReply, ref PostEntryReply);
                }
                catch (NothingReceivedException e)
                {
                    Debug.WriteLine("{" + conId + "} " + e.Message + " - " + hostname);
                    break;
                }
                catch (HTTPException e)
                {
                    reply.Status = e.Status;
                    reply.SetReply(e.Message);
                    reply.MIMEType = "text/html";
                }
                catch (Exception e)
                {
                    persistent = false;
                    Debug.WriteLine("{" + conId + "} " + e.Message + " - " + hostname);
                    break;
                }
                finally
                {
                    try
                    {
                        await SendHTTP(conId, reply, response, hostname);
                    }
                    catch (Exception)
                    {
                        persistent = false;
                    }
                }
            }
        }
Пример #3
0
        protected void HandleMethod(ref HTTPRequest request, ref HTTPReply reply, ref JSONReply GetEntryReply, ref JSONReply PostEntryReply)
        {
            switch (request.Method.Method.ToUpper())
            {
            case "HEAD":
                if (PostEntryReply != null)
                {
                    goto case "POST";
                }
                if (GetEntryReply != null)
                {
                    goto case "GET";
                }
                break;

            case "POST":
                if (PostEntryReply == null)
                {
                    throw new HTTPException(HttpStatusCode.NotFound, request.URLPath + "<br />Not Found<br />Incorrect URL");
                }
                else if (PostEntryReply.MIMEType == "application/json")
                {
                    reply.Status = PostEntryReply.Status;
                    reply.SetReply(string.Format(ReplyFormat, PostEntryReply.Message));
                }
                else
                {
                    reply.Status = PostEntryReply.Status;
                    reply.SetReply(PostEntryReply.Message);
                    reply.MIMEType = PostEntryReply.MIMEType;
                }
                break;

            case "GET":
                if (GetEntryReply == null)
                {
                    throw new HTTPException(HttpStatusCode.NotFound, request.URLPath + "<br />Not Found<br />Incorrect URL");
                }
                else if (GetEntryReply.MIMEType == "application/json")
                {
                    reply.Status = GetEntryReply.Status;
                    reply.SetReply(string.Format(ReplyFormat, GetEntryReply.Message));
                }
                else
                {
                    reply.Status = GetEntryReply.Status;
                    reply.SetReply(GetEntryReply.Message);
                    reply.MIMEType = GetEntryReply.MIMEType;
                }
                break;

            case "OPTIONS":
                reply.Status = HttpStatusCode.Ok;
                reply.SetReply("");
                reply.Header += "Allow: HEAD,GET,OPTIONS";
                reply.Header += "Access-Control-Allow-Methods: HEAD,GET,OPTIONS";
                break;

            default:
                throw new HTTPException(HttpStatusCode.NotImplemented, "Method not implemented");
            }
        }