C # implement a simple HTTP server,
Introduction
This article uses C # To implement a simple HTTP server class. You can embed it into your project or read the code to learn about HTTP.
Background
High-performance WEB applications are generally deployed on powerful WEB servers, such as IIS, Apache, and Tomcat. However, HTML is a very flexible UI markup language. That is to say, any application or backend service can provide HTML generation support. In this small example, servers such as IIS and Apache consume too much resources. We need to implement a simple HTTP server by ourselves, embed it into our application to process WEB requests. We only need one class to implement it, which is very simple.
Code Implementation
First, let's review how to use classes, and then analyze the implementation details. Here we have created a class that inherits from HttpServer and implemented the two Abstract METHODS handleGETRequest and handlePOSTRequest:
1234567891011121314151617181920212223242526 |
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( "); 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( "); p.outputStream.WriteLine( "<a href=/test>return</a><p>" ); p.outputStream.WriteLine( "postbody: <pre>{0}</pre>" , data); } } |
When processing a simple request, we need to start a separate thread to listen to a port, such as port 8080:
123 |
HttpServer httpServer = new MyHttpServer(8080); Thread thread = new Thread( new ThreadStart(httpServer.listen)); thread.Start(); |
If you compile and run this project, you will see the sample content generated on the page under the http: // localhost: 8080 address of the browser. Let's take a brief look at how the HTTP server engine is implemented.
The WEB server consists of two components. One is to start TcpListener to listen to the HttpServer class of the specified port, and use the AcceptTcpClient () method to process TCP connection requests cyclically, this is the first step to process TCP connections. After the request arrives at the "specified" port, a new port is created to initialize the TCP connection from the client to the server. This is the session of TcpClient, so that our master port can continue to receive new connection requests. From the following code, we can see that each listener creates a new TcpClien, And the HttpServer class creates a new HttpProcessor, and then starts a thread to operate. The HttpServer class also contains two abstract methods. You must implement these two methods.
12345678910111213141516171819202122232425 |
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); } |
In this way, a new tcp connection is processed by HttpProcessor in its own thread. The task of HttpProcessor is to correctly parse the HTTP header and control the correct abstract method. Let's take a look at the HTTP header processing process. The first line of code for the HTTP request is as follows:
After the input and output of process () are set, HttpProcessor calls the parseRequest () method.
123456789101112 |
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 requests are composed of three parts, so we only need to use string. the Split () method divides them into three parts. The next step is to receive and parse the HTTP header information from the client. each row of data in the header information is based on Key-Value (Key-Value) save in the form. Empty lines indicate the end mark of the HTTP header information. We use the readHeaders method in the Code to read the HTTP header information:
123456789101112131415161718192021222324 |
public void readHeaders() { Console.WriteLine( "readHeaders()" ); String line; while ((line = inputStream.ReadLine()) != 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++; // Filter out all spaces } string value = line.Substring(pos, line.Length - pos); Console.WriteLine( "header: {0}:{1}" ,name,value); httpHeaders[name] = value; } } |
Here, we have learned how to handle simple GET and POST requests, which are allocated to the correct handler respectively. In this example, there is a tricky problem to handle when sending data, that is, the request header information contains the length information of the sent data content-length, when we want the handlePOSTRequest method in the subclass HttpServer to correctly process data, we need to put the content-length information of the Data length into the data stream together, otherwise, the sender will wait for the data that will never arrive and block the waiting. We use a method that looks less elegant but very effective to handle this situation. Before sending data to the POST method, we first read the data into the MemoryStream. This approach is not ideal for the following reasons: if the data sent is large, or even a file is uploaded, it is inappropriate or even impossible to cache the data in the memory. The ideal method is to limit the post length. For example, we can limit the Data Length to 10 MB.
The simplified HTTP server also simplifies the returned content-type value. In HTTP, the server always sends the MIME-Type of the data to the client, tell the client what type of data it needs to receive. In the writeSuccess () method, we can see that the server always sends the text/html type. If you need to add other types, you can extend this method.