C#實現一個最簡單的HTTP服務器


簡介

  本文用C#實現了一個最簡單的HTTP服務器類,你可以將它嵌入到自己的項目中,或者也可以閱讀代碼來學習關於HTTP協議的知識。

 背景

  高性能的WEB應用一般都架設在強大的WEB服務器上,例如IIS, Apache, 和Tomcat。然而,HTML是非常靈活的UI標記語言,也就是說任何應用和后端服務都可以提供HTML的生成支持。在這個小小的例子中,像IIS,、Apache這樣的服務器消耗的資源太大了,我們需要自己實現一個簡單的HTTP服務器,將它嵌入到我們的應用中用來處理WEB請求。我們僅需要一個類就可以實現了,很簡單。

 代碼實現

  首先我們來回顧一下如何使用類,然后我們再來分析實現的具體細節。這里我們創建了一個繼承於HttpServer的類,並實現了handleGETRequest 和handlePOSTRequest  這兩個抽象方法:

public class MyHttpServer : HttpServer {

 public MyHttpServer(intport):base(port)
 {

    }

    public override void handleGETRequest(HttpProcessor p) {

        Console.WriteLine("request: {0}", p.http_url);

        p.writeSuccess();

        p.outputStream.WriteLine("<html><body><h1>test server</h1>");

        p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString());

        p.outputStream.WriteLine("url : {0}", p.http_url);

 

        p.outputStream.WriteLine("<form method=post action=/form>");

        p.outputStream.WriteLine("<input type=text name=foo value=foovalue>");

        p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>");

        p.outputStream.WriteLine("</form>");

    }

 

    public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) {

        Console.WriteLine("POST request: {0}", p.http_url);

        stringdata = inputData.ReadToEnd();

        p.outputStream.WriteLine("<html><body><h1>test server</h1>");

        p.outputStream.WriteLine("<a href=/test>return</a><p>");

        p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data);

    }

}

當開始處理一個簡單的請求時,我們就需要單獨啟動一個線程來監聽一個端口,比如8080端口:

HttpServer httpServer = new MyHttpServer(8080);

Thread  thread = new Thread(newThreadStart(httpServer.listen));

thread.Start();

如果你編譯運行這個項目,你會在瀏覽器http://localhost:8080地址下看到頁面上生成的示例內容。讓我們來簡單看一下這個HTTP服務器引擎是怎么實現的。

  這個WEB服務器由兩個組件構成,一個是負責啟動TcpListener來監聽指定端口的HttpServer類,並且用AcceptTcpClient()方法循環處理TCP連接請求,這是處理TCP連接的第一步。然后請求到達“已指定“的端口,接着就會創建一對新的端口,用來初始化客戶端到服務器端的TCP連接。這對端口便是TcpClient的session,這樣就可以保持我們的主端口可以繼續接收新的連接請求。從下面的代碼中我們可以看到,每一次監聽程序都會創建一個新的TcpClien,HttpServer類又會創建一個新的HttpProcessor,然后啟動一個線程來操作。HttpServer類中還包含兩個抽象方法,你必須實現這兩個方法。

public abstract class HttpServer {

    protected int port;

    TcpListener listener;

    boolis_active = true;

    publicHttpServer(intport)
   {

        this.port = port;

    }

 

    public void listen() {

        listener = newTcpListener(port);

        listener.Start();

        while(is_active)
 {                

            TcpClient s = listener.AcceptTcpClient();

            HttpProcessor processor = newHttpProcessor(s,
this);

            Thread thread = newThread(newThreadStart(processor.process));

            thread.Start();

            Thread.Sleep(1);

        }

    }

 

    public abstract void handleGETRequest(HttpProcessor p);

    public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);

}

這樣,一個新的tcp連接就在自己的線程中被HttpProcessor處理了,HttpProcessor的工作就是正確解析HTTP頭,並且控制正確實現的抽象方法。下面我們來看看HTTP頭的處理過程,HTTP請求的第一行代碼如下:

GET /myurl HTTP/1.0

在設置完process()的輸入和輸出后,HttpProcessor就會調用parseRequest()方法。

public void parseRequest() {

    String request = inputStream.ReadLine();

    string[] tokens = request.Split(' ');

    if(tokens.Length != 3) {

        throw new Exception("invalid http request line");

    }

    http_method = tokens[0].ToUpper();

    http_url = tokens[1];

    http_protocol_versionstring = tokens[2];

 

    Console.WriteLine("starting: " + request);

}

HTTP請求由3部分組成,所以我們只需要用string.Split()方法將它們分割成3部分即可,接下來就是接收和解析來自客戶端的HTTP頭信息,頭信息中的每一行數據是以Key-Value(鍵-值)形式保存,空行表示HTTP頭信息結束標志,我們代碼中用readHeaders方法來讀取HTTP頭信息:

public void readHeaders() {

    Console.WriteLine("readHeaders()");

    String line;

    while((line = inputStream.ReadLine()) != null)
 {

        if(line.Equals(""))
 {

            Console.WriteLine("got headers");

            return;

        }

 

        intseparator = line.IndexOf(':');

        if(separator == -1) {

            throw new Exception("invalid http header line: " + line);

        }

        String name = line.Substring(0, separator);

        intpos = separator + 1;

        while((pos < line.Length) && (line[pos] == '
 '))
 {

            pos++;// 過濾掉所有空格

        }

 

        string value = line.Substring(pos, line.Length - pos);

        Console.WriteLine("header: {0}:{1}",name,value);

        httpHeaders[name] = value;

    }

}

到這里,我們已經了解了如何處理簡單的GET和POST請求,它們分別被分配給正確的handler處理程序。在本例中,發送數據的時候有一個棘手的問題需要處理,那就是請求頭信息中包含發送數據的長度信息content-length,當我們希望子類HttpServer中的handlePOSTRequest方法能夠正確處理數據時,我們需要將數據長度content-length信息一起放入數據流中,否則發送端會因為等待永遠不可能到達的數據和阻塞等待。我們用了一種看起來不那么優雅但非常有效的方法來處理這種情況,即將數據發送給POST處理方法前先把數據讀入到MemoryStream中。這種做法不太理想,原因如下:如果發送的數據很大,甚至是上傳一個文件,那么我們將這些數據緩存在內存就不那么合適甚至是不可能的。理想的方法是限制post的長度,比如我們可以將數據長度限制為10MB。

  這個簡易版HTTP服務器另一個簡化的地方就是content-type的返回值,在HTTP協議中,服務器總是會將數據的MIME-Type發送給客戶端,告訴客戶端自己需要接收什么類型的數據。在writeSuccess()方法中,我們看到,服務器總是發送text/html類型,如果你需要加入其他的類型,你可以擴展這個方法。

 

完整代碼:

using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Bend.Util {

    public class HttpProcessor {
        public TcpClient socket;        
        public HttpServer srv;

        private Stream inputStream;
        public StreamWriter outputStream;

        public String http_method;
        public String http_url;
        public String http_protocol_versionstring;
        public Hashtable httpHeaders = new Hashtable();


        private static int MAX_POST_SIZE = 10 * 1024 * 1024; // 10MB

        public HttpProcessor(TcpClient s, HttpServer srv) {
            this.socket = s;
            this.srv = srv;                   
        }
        

        private string streamReadLine(Stream inputStream) {
            int next_char;
            string data = "";
            while (true) {
                next_char = inputStream.ReadByte();
                if (next_char == '\n') { break; }
                if (next_char == '\r') { continue; }
                if (next_char == -1) { Thread.Sleep(1); continue; };
                data += Convert.ToChar(next_char);
            }            
            return data;
        }
        public void process() {                        
            // we can't use a StreamReader for input, because it buffers up extra data on us inside it's
            // "processed" view of the world, and we want the data raw after the headers
            inputStream = new BufferedStream(socket.GetStream());

            // we probably shouldn't be using a streamwriter for all output from handlers either
            outputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
            try {
                parseRequest();
                readHeaders();
                if (http_method.Equals("GET")) {
                    handleGETRequest();
                } else if (http_method.Equals("POST")) {
                    handlePOSTRequest();
                }
            } catch (Exception e) {
                Console.WriteLine("Exception: " + e.ToString());
                writeFailure();
            }
            outputStream.Flush();
            // bs.Flush(); // flush any remaining output
            inputStream = null; outputStream = null; // bs = null;            
            socket.Close();             
        }

        public void parseRequest() {
            String request = streamReadLine(inputStream);
            string[] tokens = request.Split(' ');
            if (tokens.Length != 3) {
                throw new Exception("invalid http request line");
            }
            http_method = tokens[0].ToUpper();
            http_url = tokens[1];
            http_protocol_versionstring = tokens[2];

            Console.WriteLine("starting: " + request);
        }

        public void readHeaders() {
            Console.WriteLine("readHeaders()");
            String line;
            while ((line = streamReadLine(inputStream)) != null) {
                if (line.Equals("")) {
                    Console.WriteLine("got headers");
                    return;
                }
                
                int separator = line.IndexOf(':');
                if (separator == -1) {
                    throw new Exception("invalid http header line: " + line);
                }
                String name = line.Substring(0, separator);
                int pos = separator + 1;
                while ((pos < line.Length) && (line[pos] == ' ')) {
                    pos++; // strip any spaces
                }
                    
                string value = line.Substring(pos, line.Length - pos);
                Console.WriteLine("header: {0}:{1}",name,value);
                httpHeaders[name] = value;
            }
        }

        public void handleGETRequest() {
            srv.handleGETRequest(this);
        }

        private const int BUF_SIZE = 4096;
        public void handlePOSTRequest() {
            // this post data processing just reads everything into a memory stream.
            // this is fine for smallish things, but for large stuff we should really
            // hand an input stream to the request processor. However, the input stream 
            // we hand him needs to let him see the "end of the stream" at this content 
            // length, because otherwise he won't know when he's seen it all! 

            Console.WriteLine("get post data start");
            int content_len = 0;
            MemoryStream ms = new MemoryStream();
            if (this.httpHeaders.ContainsKey("Content-Length")) {
                 content_len = Convert.ToInt32(this.httpHeaders["Content-Length"]);
                 if (content_len > MAX_POST_SIZE) {
                     throw new Exception(
                         String.Format("POST Content-Length({0}) too big for this simple server",
                           content_len));
                 }
                 byte[] buf = new byte[BUF_SIZE];              
                 int to_read = content_len;
                 while (to_read > 0) {  
                     Console.WriteLine("starting Read, to_read={0}",to_read);

                     int numread = this.inputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read));
                     Console.WriteLine("read finished, numread={0}", numread);
                     if (numread == 0) {
                         if (to_read == 0) {
                             break;
                         } else {
                             throw new Exception("client disconnected during post");
                         }
                     }
                     to_read -= numread;
                     ms.Write(buf, 0, numread);
                 }
                 ms.Seek(0, SeekOrigin.Begin);
            }
            Console.WriteLine("get post data end");
            srv.handlePOSTRequest(this, new StreamReader(ms));

        }

        public void writeSuccess() {
            outputStream.WriteLine("HTTP/1.0 200 OK");            
            outputStream.WriteLine("Content-Type: text/html;charset=utf-8");
            outputStream.WriteLine("Connection: close");
            outputStream.WriteLine("");
        }

        public void writeFailure() {
            outputStream.WriteLine("HTTP/1.0 404 File not found");
            outputStream.WriteLine("Connection: close");
            outputStream.WriteLine("");
        }
    }

    public abstract class HttpServer {

        protected int port;
        TcpListener listener;
        bool is_active = true;
       
        public HttpServer(int port) {
            this.port = port;
        }

        public void listen() {
            listener = new TcpListener(port);
            listener.Start();
            while (is_active) {                
                TcpClient s = listener.AcceptTcpClient();
                HttpProcessor processor = new HttpProcessor(s, this);
                Thread thread = new Thread(new ThreadStart(processor.process));
                thread.Start();
                Thread.Sleep(1);
            }
        }

        public abstract void handleGETRequest(HttpProcessor p);
        public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);
    }

    public class MyHttpServer : HttpServer {
        public MyHttpServer(int port)
            : base(port) {
        }
        public override void handleGETRequest(HttpProcessor p) {
            Console.WriteLine("request: {0}", p.http_url);
            p.writeSuccess();
            p.outputStream.WriteLine("<html><body><h1>test server</h1>");
            p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString());
            p.outputStream.WriteLine("url : {0}", p.http_url);

            p.outputStream.WriteLine("<form method=post action=/form>");
            p.outputStream.WriteLine("<input type=text name=foo value=foovalue>");
            p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>");
            p.outputStream.WriteLine("</form>");
        }

        public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) {
            Console.WriteLine("POST request: {0}", p.http_url);
            string data = inputData.ReadToEnd();

            p.outputStream.WriteLine("<html><body><h1>test server</h1>");
            p.outputStream.WriteLine("<a href=/test>return</a><p>");
            p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data);
            

        }
    }

    public class TestMain {
        public static int Main(String[] args) {
            HttpServer httpServer;
            if (args.GetLength(0) > 0) {
                httpServer = new MyHttpServer(Convert.ToInt16(args[0]));
            } else {
                httpServer = new MyHttpServer(8080);
            }
            Thread thread = new Thread(new ThreadStart(httpServer.listen));
            thread.Start();
            return 0;
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM