Ejemplo n.º 1
0
        public static bool HandleIfNotModified(this HttpConnection p, DateTime lastModified, string etag, string lastModifiedString)
        {
            if (lastModified != null)
            {
                p.setHeader("Last-Modified", lastModifiedString);
            }
            if (etag != null)
            {
                p.setHeader("Etag", etag);
            }
            //p.setHeader("Cache-Control", "public,max-age=3600");
            var notmod = (etag != null && p.GetReqHeader("If-None-Match") == etag);

            if (!notmod)
            {
                if (lastModified > DateTime.MinValue)
                {
                    var sinceStr = p.GetReqHeader("If-Modified-Since");
                    if (sinceStr != null && DateTime.TryParse(sinceStr, out var since))
                    {
                        if (since <= lastModified)
                        {
                            notmod = true;
                        }
                    }
                }
            }
            if (notmod)
            {
                p.ResponseStatusCode = "304 Not Modified";
            }
            return(notmod);
        }
Ejemplo n.º 2
0
        public static string Parse(HttpConnection p)
        {
            string auth = p.GetReqHeader(HttpHeaders.KEY_Authorization);

            if (auth != null && auth.StartsWith("Basic"))
            {
                var base64str = auth.Substring(5);
                return(NaiveUtils.UTF8Encoding.GetString(Convert.FromBase64String(base64str)));
            }
            return(null);
        }
Ejemplo n.º 3
0
        public async Task <WsHandleRequestResult> HandleRequestAsync(bool enterRecvLoop)
        {
            List <string> connectionSplits = p.GetReqHeaderSplits("Connection");

            if (p.Method == "GET" &&
                p.GetReqHeader("Upgrade") == "websocket" &&
                (connectionSplits.Contains("Upgrade") || connectionSplits.Contains("upgrade")))
            {
                p.Handled = true;
                if (p.GetReqHeader("Sec-WebSocket-Version") != "13")
                {
                    p.ResponseStatusCode = "400 Bad Request";
                    p.setHeader("Sec-WebSocket-Version", "13");
                    return(new WsHandleRequestResult(WsHandleRequestResult.Results.BadVersion));
                }
                var wskey = p.GetReqHeader("Sec-WebSocket-Key");
                if (wskey == null)
                {
                    p.ResponseStatusCode = "400 Bad Request";
                    p.keepAlive          = false;
                    return(new WsHandleRequestResult(WsHandleRequestResult.Results.BadKey));
                }

                p.ResponseStatusCode = "101 Switching Protocols";
                p.setHeader("Upgrade", "websocket");
                p.setHeader("Connection", "Upgrade");
                p.setHeader("Sec-WebSocket-Accept", GetWebsocketAcceptKey(wskey));
                p.keepAlive = false;
                await p.EndResponseAsync().CAF();

                p.SwitchProtocol();
                ConnectionState = States.Open;
                if (enterRecvLoop)
                {
                    await recvLoopAsync().CAF();
                }
                return(new WsHandleRequestResult(WsHandleRequestResult.Results.Connected));
            }
            return(new WsHandleRequestResult(WsHandleRequestResult.Results.NonWebsocket));
        }
Ejemplo n.º 4
0
        public static bool Check(HttpConnection p)
        {
            var contentType = p.GetReqHeader(HttpHeaders.KEY_Content_Type);

            if (contentType == null)
            {
                return(false);
            }
            if (!contentType.StartsWith("multipart/form-data;"))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 5
0
        private static byte[] GetBoundary(HttpConnection p)
        {
            const string boundary    = "boundary=";
            var          contentType = p.GetReqHeader(HttpHeaders.KEY_Content_Type);

            if (contentType == null)
            {
                throw new Exception("no Content-Type");
            }
            if (!contentType.StartsWith("multipart/form-data;"))
            {
                throw new Exception("!Content-Type.StartWith(\"multipart/form-data\")");
            }
            var boundrayIndex = contentType.IndexOf(boundary);

            if (boundrayIndex == -1)
            {
                throw new Exception("'boundary=' not Found");
            }
            return(NaiveUtils.UTF8Encoding.GetBytes("--" + contentType.Substring(boundrayIndex + boundary.Length)));
        }
Ejemplo n.º 6
0
 public static bool IsWebSocketRequest(HttpConnection p) => p.GetReqHeader("Upgrade") == "websocket";