HttpProcessor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. namespace ZLPlugin_LisPacs_MR_YJMZ
  10. {
  11. public class HttpProcessor
  12. {
  13. public TcpClient socket;
  14. public HttpServer srv;
  15. private Stream inputStream;
  16. public StreamWriter outputStream;
  17. public String http_method;
  18. public String http_url;
  19. public String http_protocol_versionstring;
  20. public Hashtable httpHeaders = new Hashtable();
  21. private static int MAX_POST_SIZE = 10 * 1024 * 1024; // 10MB
  22. public HttpProcessor(TcpClient s, HttpServer srv)
  23. {
  24. this.socket = s;
  25. this.srv = srv;
  26. }
  27. private string streamReadLine(Stream inputStream)
  28. {
  29. int next_char;
  30. string data = "";
  31. while (true)
  32. {
  33. next_char = inputStream.ReadByte();
  34. if (next_char == '\n') { break; }
  35. if (next_char == '\r') { continue; }
  36. if (next_char == -1) { Thread.Sleep(1); continue; };
  37. data += Convert.ToChar(next_char);
  38. }
  39. return data;
  40. }
  41. public void process()
  42. {
  43. // we can't use a StreamReader for input, because it buffers up extra data on us inside it's
  44. // "processed" view of the world, and we want the data raw after the headers
  45. inputStream = new BufferedStream(socket.GetStream());
  46. // we probably shouldn't be using a streamwriter for all output from handlers either
  47. outputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
  48. try
  49. {
  50. parseRequest();
  51. readHeaders();
  52. if (http_method.Equals("GET"))
  53. {
  54. handleGETRequest();
  55. }
  56. else if (http_method.Equals("POST"))
  57. {
  58. handlePOSTRequest();
  59. }
  60. }
  61. catch (Exception e)
  62. {
  63. Console.WriteLine("Exception: " + e.ToString());
  64. writeFailure();
  65. }
  66. outputStream.Flush();
  67. // bs.Flush(); // flush any remaining output
  68. inputStream = null; outputStream = null; // bs = null;
  69. socket.Close();
  70. }
  71. public void parseRequest()
  72. {
  73. String request = streamReadLine(inputStream);
  74. string[] tokens = request.Split(' ');
  75. if (tokens.Length != 3)
  76. {
  77. throw new Exception("invalid http request line");
  78. }
  79. http_method = tokens[0].ToUpper();
  80. http_url = tokens[1];
  81. http_protocol_versionstring = tokens[2];
  82. Console.WriteLine("starting: " + request);
  83. }
  84. public void readHeaders()
  85. {
  86. Console.WriteLine("readHeaders()");
  87. String line;
  88. while ((line = streamReadLine(inputStream)) != null)
  89. {
  90. if (line.Equals(""))
  91. {
  92. Console.WriteLine("got headers");
  93. return;
  94. }
  95. int separator = line.IndexOf(':');
  96. if (separator == -1)
  97. {
  98. throw new Exception("invalid http header line: " + line);
  99. }
  100. String name = line.Substring(0, separator);
  101. int pos = separator + 1;
  102. while ((pos < line.Length) && (line[pos] == ' '))
  103. {
  104. pos++; // strip any spaces
  105. }
  106. string value = line.Substring(pos, line.Length - pos);
  107. Console.WriteLine("header: {0}:{1}", name, value);
  108. httpHeaders[name] = value;
  109. }
  110. }
  111. public void handleGETRequest()
  112. {
  113. srv.handleGETRequest(this);
  114. }
  115. private const int BUF_SIZE = 4096;
  116. public void handlePOSTRequest()
  117. {
  118. // this post data processing just reads everything into a memory stream.
  119. // this is fine for smallish things, but for large stuff we should really
  120. // hand an input stream to the request processor. However, the input stream
  121. // we hand him needs to let him see the "end of the stream" at this content
  122. // length, because otherwise he won't know when he's seen it all!
  123. Console.WriteLine("get post data start");
  124. int content_len = 0;
  125. MemoryStream ms = new MemoryStream();
  126. if (this.httpHeaders.ContainsKey("Content-Length"))
  127. {
  128. content_len = Convert.ToInt32(this.httpHeaders["Content-Length"]);
  129. if (content_len > MAX_POST_SIZE)
  130. {
  131. throw new Exception(
  132. String.Format("POST Content-Length({0}) too big for this simple server",
  133. content_len));
  134. }
  135. byte[] buf = new byte[BUF_SIZE];
  136. int to_read = content_len;
  137. while (to_read > 0)
  138. {
  139. Console.WriteLine("starting Read, to_read={0}", to_read);
  140. int numread = this.inputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read));
  141. Console.WriteLine("read finished, numread={0}", numread);
  142. if (numread == 0)
  143. {
  144. if (to_read == 0)
  145. {
  146. break;
  147. }
  148. else
  149. {
  150. throw new Exception("client disconnected during post");
  151. }
  152. }
  153. to_read -= numread;
  154. ms.Write(buf, 0, numread);
  155. }
  156. ms.Seek(0, SeekOrigin.Begin);
  157. }
  158. Console.WriteLine("get post data end");
  159. srv.handlePOSTRequest(this, new StreamReader(ms));
  160. }
  161. public void writeSuccess()
  162. {
  163. outputStream.WriteLine("HTTP/1.0 200 OK");
  164. outputStream.WriteLine("Content-Type: text/html");
  165. outputStream.WriteLine("Connection: close");
  166. outputStream.WriteLine("");
  167. }
  168. public void writeFailure()
  169. {
  170. outputStream.WriteLine("HTTP/1.0 404 File not found");
  171. outputStream.WriteLine("Connection: close");
  172. outputStream.WriteLine("");
  173. }
  174. }
  175. }