00001 using System;
00002 using Common;
00003
00004 namespace Client {
00005
00010 public interface IResponseHandler {
00015 void Handle(Message responseMsg);
00016 }
00017
00018
00024 public class HTTPResponseHandler : IResponseHandler {
00026 Connection client;
00027 ClientControl clientControl;
00028 HTTPRequest originalRequest;
00029
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 public HTTPResponseHandler(HTTPRequest originalRequest, Connection client, ClientControl clientControl) {
00041 this.originalRequest = originalRequest;
00042 this.client = client;
00043 this.clientControl = clientControl;
00044 }
00045
00052 public void Handle(Message msg) {
00053 HTTPResponse response;
00054 if (msg.Type == MessageType.HTTPResponse) {
00055 HTTPResponseMessage rspMsg = (HTTPResponseMessage)msg;
00056
00057 try {
00058 response = rspMsg.encodedResponse.Decode(clientControl.clientCacheManager);
00059
00060 clientControl.clientCacheManager.UpdateCache(response, client.Request.URI);
00061 if (clientControl.settings.PendingRequestsEnabled) {
00062 if (response != null && response.ContentType != null && response.ContentType.StartsWith("text/html")) {
00063 if (response.Body != null) {
00064 clientControl.serverStub.pendingRequestManager.AddPlaceholders(ImageUriParser.GetImageUris(clientControl.msgLog, client.Request.URI, response.Body.data));
00065 }
00066 }
00067 }
00068 client.SendResponse(response);
00069 } catch (CacheNotFoundException ex) {
00070 Console.WriteLine("***" + ex.Message + "***");
00071 Console.WriteLine("* Re-issuing index message and original request");
00072 clientControl.serverStub.SendCacheIndexMessage(clientControl.clientCacheManager.GetCacheIndexMessage());
00073 clientControl.serverStub.ServiceReq(originalRequest, client);
00074 }
00075 } else if (msg.Type == MessageType.NoChange) {
00076 NoChangeMessage noChangeMsg = (NoChangeMessage)msg;
00077
00078 if (noChangeMsg.chk != null && clientControl.clientCacheManager.CacheContains(noChangeMsg.chk)) {
00079
00080
00081 clientControl.clientCacheManager.UpdateURIMapping(originalRequest.URI, noChangeMsg.chk, noChangeMsg.responseHeaders.rawString);
00082
00083 response = clientControl.clientCacheManager.Get(originalRequest);
00084
00085 client.SendResponse(response);
00086 } else {
00087
00088 clientControl.serverStub.SendCacheIndexMessage(clientControl.clientCacheManager.GetCacheIndexMessage());
00089 if (noChangeMsg.chk != null) {
00090 clientControl.serverStub.ServiceReq(originalRequest, client);
00091 }
00092 }
00093 } else if (msg.Type == MessageType.CacheUpdateHTTPResponse || msg.Type == MessageType.CacheUpdateNoChange) {
00094
00095
00096 clientControl.msgLog.Log("Handling previously pending request for {0}", originalRequest.URI);
00097 clientControl.clientCacheManager.ServiceReq(originalRequest, client);
00098 } else {
00099 throw new ApplicationException(String.Format("Unkown message type {0} in HTTPResponseHandler.Handle", msg.Type.ToString()));
00100 }
00101 }
00102
00104 public Uri RequestUri {
00105 get { return originalRequest.URI; }
00106 }
00107 }
00108
00109 }