addHeader() public method

public addHeader ( String field, String data ) : void
field String
data String
return void
Exemplo n.º 1
0
        /// <summary>
        /// This method checks that the request path exists in the PAGE_ROOT directory.
        /// Then the content-type is parsed from the request.
        /// An HttpRequest is generated and sent, followed by the file specified.
        /// Call this method conditionally from any subclass unless there are no conditions as to what clients are permitted to GET.
        /// </summary>
        /// <param name="request"></param>
        protected virtual bool processGet(HttpRequest request)
        {
            String relativePath = request.Path;

            if (relativePath.IndexOf('/') == 0)
            {
                relativePath = relativePath.Substring(1);
            }
            HttpResponse response;

            //Keep in mind that File.OpenRead defaultly uses FileShare.Read
            #region "File Checks and Response Generation"
            if (File.Exists(PAGE_ROOT + relativePath))
            {
                String Content = request.requestMetaInfo("Accept");
                response = new HttpResponse(HttpResponse.ConnectionStatus.OK, "keep-alive", PAGE_ROOT + relativePath);
                response.addHeader("Content-Type", request.getGuessFileType());
            }
            else
            {
                if (request.Path.Equals("/"))
                {
                    response = new HttpResponse(HttpResponse.ConnectionStatus.OK, "keep-alive", PAGE_ROOT + @"\" + onInit(request));
                    response.addHeader("Content-Type", "text/html");
                }
                else
                {
                    on404(request);
                    return(false);
                }
            }
            #endregion


            using (FileStream s = File.OpenRead(response.FilePath))
            {
                response.addHeader("Content-Length", s.Length.ToString());
                try
                {
                    SocketWriteLine(response.ToString());
                    s.CopyTo(stream);
                }
                catch (SocketException)
                {
                    return(false);
                }
                catch (IOException)
                {
                    return(false);
                }
            }
            //}
            //catch (IOException e)
            //{
            //    Debug.WriteLine("send failure:  " + request.path);
            //}

            stream.Flush();
            return(true);
        }
Exemplo n.º 2
0
        protected void writeStream(Stream s, HttpResponse response)
        {
            try
            {
                response.addHeader("Content-Length", s.Length.ToString());
                SocketWriteLine(response.ToString());
                s.CopyTo(stream);
            }
            catch (IOException) { }
            s.Close();

            stream.Flush();
        }
Exemplo n.º 3
0
        protected void writeHtmlStream(Stream s, HttpResponse response)
        {
            String html;

            try
            {
                byte[] codedHtml = new byte[s.Length];
                s.Read(codedHtml, 0, (int)s.Length);
                html = Encoding.UTF8.GetString(codedHtml);
                foreach (KeyValuePair <string, string> kp in pageMutations)
                {
                    html = html.Replace(kp.Key, kp.Value);
                }
                response.addHeader("Content-Length", html.Length.ToString());
                SocketWriteLine(response.ToString());
                SocketWriteText(html);
            }
            catch (IOException) { }
            s.Close();

            stream.Flush();
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method checks that the request path exists in the PAGE_ROOT directory.
        /// Then the content-type is parsed from the request.
        /// An HttpRequest is generated and sent, followed by the file specified.
        /// Call this method conditionally from any subclass unless there are no conditions as to what clients are permitted to GET.
        /// </summary>
        /// <param name="request"></param>
        protected virtual bool processGet(HttpRequest request)
        {
            String relativePath = request.Path;
            if (relativePath.IndexOf('/') == 0)
            {
                relativePath = relativePath.Substring(1);
            }
            HttpResponse response;

            //Keep in mind that File.OpenRead defaultly uses FileShare.Read
            #region "File Checks and Response Generation"
            if (File.Exists(PAGE_ROOT + relativePath))
            {
                String Content = request.requestMetaInfo("Accept");
                response = new HttpResponse(HttpResponse.ConnectionStatus.OK, "keep-alive", PAGE_ROOT + relativePath);
                response.addHeader("Content-Type", request.getGuessFileType());
            }
            else
            {
                if (request.Path.Equals("/"))
                {
                    response = new HttpResponse(HttpResponse.ConnectionStatus.OK, "keep-alive", PAGE_ROOT + @"\" + onInit(request));
                    response.addHeader("Content-Type", "text/html");
                }
                else
                {
                    on404(request);
                    return false;
                }
            }
            #endregion

            using (FileStream s = File.OpenRead(response.FilePath))
            {
                response.addHeader("Content-Length", s.Length.ToString());
                try
                {
                    SocketWriteLine(response.ToString());
                    s.CopyTo(stream);
                }
                catch (SocketException)
                {
                    return false;
                }
                catch (IOException)
                {
                    return false;
                }
            }
            //}
            //catch (IOException e)
            //{
            //    Debug.WriteLine("send failure:  " + request.path);
            //}

            stream.Flush();
            return true;
        }
Exemplo n.º 5
0
        protected void writeStream(Stream s, HttpResponse response)
        {
            try
            {
                response.addHeader("Content-Length", s.Length.ToString());
                SocketWriteLine(response.ToString());
                s.CopyTo(stream);
            }
            catch (IOException) { }
            s.Close();

            stream.Flush();
        }
Exemplo n.º 6
0
        protected void writeHtmlStream(Stream s, HttpResponse response)
        {
            String html;
            try
            {
                byte[] codedHtml = new byte[s.Length];
                s.Read(codedHtml, 0, (int)s.Length);
                html = Encoding.UTF8.GetString(codedHtml);
                foreach (KeyValuePair<string, string> kp in pageMutations)
                {
                    html = html.Replace(kp.Key, kp.Value);
                }
                response.addHeader("Content-Length", html.Length.ToString());
                SocketWriteLine(response.ToString());
                SocketWriteText(html);
            }
            catch (IOException) { }
            s.Close();

            stream.Flush();
        }