Main Page | Class Hierarchy | Class List | File List | Class Members

HTTPStub.cs

00001 using System;
00002 using System.IO;
00003 using System.Threading;
00004 using Common;
00005 using System.Net;
00006 using System.Net.Sockets;
00007 
00008 namespace Server
00009 {
00013         public class HTTPStub {
00014                 public bool usingProxy;
00015                 public string proxyHost;
00016                 public int proxyPort;
00017                 public int sendTimeoutMS;
00018                 public int receiveTimeoutMS;
00019 
00020                 public void Init() {
00021                         usingProxy = false;
00022                         proxyHost = "wwwcache.cam.ac.uk";
00023                         proxyPort = 8080;
00024                         sendTimeoutMS = 15 * 1000;
00025                         receiveTimeoutMS = 30 * 1000;
00026                 }
00027 
00028                 public HTTPStub() {
00029                         Init();
00030                 }
00031 
00032                 public HTTPResponseQueueObject ServiceRequest(HTTPRequest req) {
00033                         HTTPResponseQueueObject responseQObj = null;
00034                         Socket sock = null;
00035                         try {
00036                                 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
00037                                 IPEndPoint remoteEndPoint;
00038                                 if (usingProxy) {
00039                                         remoteEndPoint = new IPEndPoint(Dns.GetHostByName(proxyHost).AddressList[0], proxyPort);
00040                                 } else {
00041                                         remoteEndPoint = new IPEndPoint(Dns.GetHostByName(req.Hostname).AddressList[0], req.Port);
00042                                 }
00043                                 sock.Connect(remoteEndPoint);
00044                                 sock.Blocking = true;
00045                                 sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, receiveTimeoutMS);
00046                                 sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, sendTimeoutMS);
00047                                 byte[] outputBuffer = req.ToHTTPByteArray();
00048                                 int bytesSent = sock.Send(outputBuffer);
00049                                 if (bytesSent != outputBuffer.Length) {
00050                                         throw new ApplicationException("Byte send mismatch");
00051                                 }
00052                                 //sock.Shutdown(SocketShutdown.Send);
00053 
00054                                 // build up the response
00055                                 byte[] buffer = new byte[8 * 1024];                             
00056                                 MemoryStream ms = new MemoryStream();
00057                                 int bytesRead = sock.Receive(buffer, buffer.Length, SocketFlags.None);  
00058                                 while (bytesRead != 0) {
00059                                         ms.Write(buffer, 0, bytesRead);
00060                                         bytesRead = sock.Receive(buffer, buffer.Length, SocketFlags.None);      
00061                                 }
00062                                 ms.Seek(0, SeekOrigin.Begin);
00063                                 Stream str = (Stream)ms;
00064                                 responseQObj = new HTTPResponseQueueObject(ref str);
00065                                 str.Close();
00066                                 sock.Shutdown(SocketShutdown.Both);
00067                                 sock.Close();
00068                         } catch (Exception ex) {
00069                                 Console.WriteLine("*** Unable to fetch response: {0} ***", ex); // if error, return browser error and message.
00070                                 string returnheaders = "HTTP/1.0 500 Server Error\r\nContent-type: text/html\r\n\r\n";
00071                                 responseQObj = new HTTPResponseQueueObject(new HTTPResponseHeader(returnheaders));
00072                                 responseQObj.AppendBody(new HTTPBody(System.Text.Encoding.ASCII.GetBytes(FormatError(ex))));
00073                         }
00074                                 return responseQObj;
00075                 }
00076 
00077                 public string FormatError(Exception ex) {
00078                         System.Text.StringBuilder sb = new System.Text.StringBuilder();
00079                         sb.Append(@"<HTML><HEAD><TITLE>GPRSWeb Error</TITLE></HEAD><BODY>");
00080                         sb.Append(String.Format(@"<H1>{0}</H1><P>An error occured when trying to fetch your document. The error message was <i>{1}</i>", ex.GetType().Name, ex.Message));
00081                         sb.Append(String.Format(@"<P>Stack Trace<P><PRE>{0}</PRE>", ex.StackTrace));
00082                         sb.Append(String.Format(@"<P>Full details: <p><PRE>{0}</PRE></p></body></html>", ex.ToString()));
00083                         return sb.ToString();
00084                 }
00085         }
00086 }

Generated on Mon May 8 22:07:27 2006 by  doxygen 1.3.9.1