一个用C#实现的简单http server

http.cs  
----------------------------   
using system;
using system.collections;
using system.io;
using system.net;
using system.net.sockets;
using system.threading;

class httpprocessor {

  private socket s;
  private bufferedstream bs;
  private streamreader sr;
  private streamwriter sw;
  private string method;
  private string url;
  private string protocol;
  private hashtable hashtable;

  public httpprocessor(socket s) {
    this.s = s;
    hashtable = new hashtable();
  }

  public void process() {
    networkstream ns = new networkstream(s, fileaccess.readwrite);
    bs = new bufferedstream(ns);
    sr = new streamreader(bs);
    sw = new streamwriter(bs);
    parserequest();
    readheaders();
    writeurl();
    s.shutdown(socketshutdown.sdboth);
    ns.close();
  }

  public void parserequest() {
    string request = sr.readline();
    string[] tokens = request.split(new char[]{ });
    method = tokens[0];
    url = tokens[1];
    protocol = tokens[2];
  }

  public void readheaders() {
    string line;
    while((line = sr.readline()) != null && line != "") {
      string[] tokens = line.split(new char[]{:});
      string name = tokens[0];
      string value = "";
      for(int i = 1; i < tokens.length; i++) {
        value += tokens[i];
        if(i < tokens.length - 1) tokens[i] += ":";
      }
      hashtable[name] = value;
    }
  }

  public void writeurl() {
    try {
      filestream fs = new filestream(url.substring(1), filemode.open, fileaccess.read);
      writesuccess();
      bufferedstream bs2 = new bufferedstream(fs);
      byte[] bytes = new byte[4096];
      int read;
      while((read = bs2.read(bytes, 0, bytes.length)) != 0) {
        bs.write(bytes, 0, read);
      }
      bs2.close();
    } catch(filenotfoundexception) {
      writefailure();
      sw.writeline("file not found: " + url);
    }
    sw.flush();
  }

  public void writesuccess() {
    sw.writeline("http/1.0 200 ok");
    sw.writeline("connection: close");
    sw.writeline();
  }

  public void writefailure() {
    sw.writeline("http/1.0 404 file not found");
    sw.writeline("connection: close");
    sw.writeline();
  }
}

public class httpserver {

  // ============================================================
  // data

  protected int port;

  // ============================================================
  // constructor

  public httpserver() : this(80) {
  }

  public httpserver(int port) {
    this.port = port;
  }

  // ============================================================
  // listener

  public void listen() {
    socket listener = new socket(0, sockettype.sockstream, protocoltype.prottcp);
    ipaddress ipaddress = new ipaddress("127.0.0.1");
    ipendpoint endpoint = new ipendpoint(ipaddress, port);
    listener.bind(endpoint);
    listener.blocking = true;
    listener.listen(-1);
    while(true) {
      socket s = listener.accept();
      httpprocessor processor = new httpprocessor(s);
      thread thread = new thread(new threadstart(processor.process));
      thread.start();
    }
  }

  // ============================================================
  // main

  public static int main(string[] args) {
    httpserver httpserver;
    if(args.getlength(0) > 0) {
      httpserver = new httpserver(args[0].touint16());
    } else {
      httpserver = new httpserver();
    }
    thread thread = new thread(new threadstart(httpserver.listen));
    thread.start();
    return 0;
  }
}

 
展开阅读全文

没有更多推荐了,返回首页