Exemplo n.º 1
0
        public static void SendFile(IHTTPContext context, String filename, String contentType)
        {
            if (!File.Exists(filename))
            {
                context.Response.SendErrorResponse(404);
                return;
            }
            String lastModified = File.GetLastWriteTimeUtc(filename).ToString("R");

            if (context.RequestHeaders["If-Modified-Since"] == lastModified)
            {
                context.Response.SendStatus(304);
                return;
            }
            if (contentType == null)
            {
                contentType = HTTPServer.GetMimeTypeForExtension(Path.GetExtension(filename));
            }
            using (FileStream fs = File.OpenRead(filename)) {
                context.Response.SendStatus(200);
                if (!String.IsNullOrEmpty(contentType))
                {
                    context.Response.SendHeader("Content-Type", contentType);
                }
                context.Response.SendHeader("Last-Modified", lastModified);
                context.Response.WriteResponseData(fs);
            }
        }
Exemplo n.º 2
0
        public void ServeRequest(IHTTPContext context)
        {
            ArraySegment <Byte> content = ContentBuffer;

            if (content.Array == null)
            {
                context.Response.SendErrorResponse(404);
                return;
            }
            String contentType = ContentType;

            context.Response.SendStatus(200);
            if (contentType != null)
            {
                context.Response.SendHeader("Content-Type", contentType);
            }
            context.Response.WriteResponseData(content.Array, content.Offset, content.Count);
        }
Exemplo n.º 3
0
        public void ServeRequest(IHTTPContext context)
        {
            if (!File.Exists(TarFileName))
            {
                context.Response.SendErrorResponse(404);
                return;
            }
            String reqname1 = context.RequestPath;

            if (reqname1.StartsWith("/"))
            {
                reqname1 = reqname1.Substring(1);
            }
            String reqname2 = reqname1;

            //Todo: use index.htm only if path ends in /; if path does not end in / and path is a directory, send 302 redirect.
            if (reqname2.Length > 0 && !reqname2.EndsWith("/"))
            {
                reqname2 += "/";
            }
            reqname2 += "index.htm";
            foreach (TarchiveEntry file in new TarchiveReader(TarFileName))
            {
                if (!file.IsFile)
                {
                    continue;
                }
                if (!reqname1.Equals(file.Name, StringComparison.OrdinalIgnoreCase) && !reqname2.Equals(file.Name, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                context.Response.SendStatus(200);
                String ctype = HTTPServer.GetMimeTypeForExtension(Path.GetExtension(file.Name));
                if (ctype != null)
                {
                    context.Response.SendHeader("Content-Type", ctype);
                }
                using (Stream source = file.GetStream()) context.Response.WriteResponseData(source);
                return;
            }
            context.Response.SendErrorResponse(404);
        }
Exemplo n.º 4
0
        public void ServeRequest(IHTTPContext context)
        {
            PrefixInfo c = Prefixes.Find(delegate(PrefixInfo item) {
                if (item.ExactMatch)
                {
                    return(context.RequestPath.Equals(item.Prefix, PrefixComparison));
                }
                else
                {
                    return(context.RequestPath.StartsWith(item.Prefix, PrefixComparison));
                }
            });

            if (c.Handler != null)
            {
                c.Handler.ServeRequest(context);
            }
            else
            {
                context.Response.SendErrorResponse(404);
            }
        }
Exemplo n.º 5
0
 public HTTPContextWrapper(IHTTPContext inner)
 {
     this.inner = inner;
 }
Exemplo n.º 6
0
 public void ServeRequest(IHTTPContext context)
 {
     Handler(context);
 }
Exemplo n.º 7
0
 public static void SendFile(IHTTPContext context, String filename)
 {
     SendFile(context, filename, null);
 }
Exemplo n.º 8
0
 public void ServeRequest(IHTTPContext context)
 {
     SendFile(context, FileName, ContentType);
 }
Exemplo n.º 9
0
 public WebSocketPacketStream(IHTTPContext context)
 {
     try {
         String   ConnectionHeader      = context.RequestHeaders["Connection"];          //can be comma-separated list
         Boolean  ConnectionUpgrade     = ConnectionHeader != null && ConnectionHeader.Contains("Upgrade");
         Boolean  UpgradeWebsocket      = "WebSocket".Equals(context.RequestHeaders["Upgrade"], StringComparison.OrdinalIgnoreCase);
         String   SecWebSocketKey       = context.RequestHeaders["Sec-WebSocket-Key"];
         String   SecWebSocketKey1      = context.RequestHeaders["Sec-WebSocket-Key1"];
         String   SecWebSocketKey2      = context.RequestHeaders["Sec-WebSocket-Key2"];
         String   SecWebSocketProtocol  = context.RequestHeaders["Sec-WebSocket-Protocol"];
         String[] SecWebSocketProtocols = SecWebSocketProtocol == null ? null : SecWebSocketProtocol.Split(',');
         if (!ConnectionUpgrade || !UpgradeWebsocket)
         {
             throw new InvalidOperationException("The HTTP context does not contain a WebSocket request");
         }
         if (SecWebSocketProtocols != null)
         {
             SecWebSocketProtocols = Array.ConvertAll(SecWebSocketProtocols, s => s.Trim().ToLowerInvariant());
         }
         binaryProtocol = SecWebSocketProtocols != null && Array.IndexOf(SecWebSocketProtocols, "binary") != -1;
         if (SecWebSocketKey != null)
         {
             wsProtocol = 13;
             String hashedKey;
             using (SHA1 sha1 = SHA1Managed.Create()) {
                 Byte[] hashable = Encoding.ASCII.GetBytes(SecWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
                 Byte[] hash     = sha1.ComputeHash(hashable);
                 hashedKey = Convert.ToBase64String(hash, Base64FormattingOptions.None);
             }
             context.Response.SendStatus(101);
             context.Response.SetResponseHeader("Connection", "Upgrade");
             context.Response.SetResponseHeader("Upgrade", "websocket");
             context.Response.SetResponseHeader("Sec-WebSocket-Accept", hashedKey);
             if (SecWebSocketProtocols != null)
             {
                 context.Response.SetResponseHeader("Sec-WebSocket-Protocol", binaryProtocol ? "binary" : "base64");
             }
             Stream rawstream = context.GetDirectStream();
             baseStream = rawstream as PrebufferingStream ?? new PrebufferingStream(rawstream);
         }
         else if (SecWebSocketKey1 != null && SecWebSocketKey2 != null)
         {
             wsProtocol = 100;
             Byte[] key = new Byte[4 + 4 + 8];
             CalculateHybi00MagicNumber(SecWebSocketKey1, key, 0);
             CalculateHybi00MagicNumber(SecWebSocketKey2, key, 4);
             context.Response.SendStatus(101);
             context.Response.SetResponseHeader("Connection", "Upgrade");
             context.Response.SetResponseHeader("Upgrade", "websocket");
             if (SecWebSocketProtocols != null)
             {
                 context.Response.SetResponseHeader("Sec-WebSocket-Protocol", binaryProtocol ? "binary" : "base64");
             }
             context.Response.SendHeader("Sec-WebSocket-Origin", context.RequestHeaders["Origin"]);
             context.Response.SendHeader("Sec-WebSocket-Location", (context.IsSecure ? "wss://" : "ws://") + context.RequestHeaders["Host"] + context.RequestPath);
             Stream rawstream = context.GetDirectStream();
             baseStream = rawstream as PrebufferingStream ?? new PrebufferingStream(rawstream);
             baseStream.ReadAll(key, 8, 8);
             using (MD5 md5 = MD5.Create()) key = md5.ComputeHash(key);
             baseStream.Write(key, 0, key.Length);
         }
         else
         {
             throw new InvalidOperationException("Unsupported WebSocket request");
         }
     } catch (Exception) {
         closed = true;
         if (baseStream != null)
         {
             baseStream.Close();
         }
         throw;
     }
 }