public void TestBypassException_pathWithQueryString() { using (HttpServer proxyServer = new HttpServer(80)) { proxyServer.AddHandler("/", new MyHandler("dummy")); // dummy BypassHttpHandler bypasser = new BypassHttpHandler("http://localhost:8080"); bypasser.AddExceptPrefix("/bypass/"); proxyServer.AddDefaultHandler(bypasser); proxyServer.Start(); WebClient agent = new WebClient(); try { agent.DownloadData("http://localhost:80/bypass?abc=123"); } catch (WebException e) { Assert.AreEqual(WebExceptionStatus.ProtocolError, e.Status); using (StreamReader r = new StreamReader(e.Response.GetResponseStream())) { string json = r.ReadToEnd(); CloudResponse resp = fastJSON.JSON.Instance.ToObject <CloudResponse>(json); Assert.AreEqual(-1, resp.api_ret_code); Assert.AreEqual(403, resp.status); Assert.AreEqual("Station does not support this REST API; only Cloud does", resp.api_ret_message); } return; } Assert.Fail("Expected exception is not thrown"); } }
public void TestBypassRemoteError_no_response_stream() { using (HttpServer proxyServer = new HttpServer(80)) using (HttpServer targetServer = new HttpServer(8080)) { proxyServer.AddHandler("/", new MyHandler("dummy")); // dummy BypassHttpHandler bypasser = new BypassHttpHandler("http://localhost:8080"); proxyServer.AddDefaultHandler(bypasser); proxyServer.Start(); targetServer.AddHandler("/bypass/", new ErrorHttpHandler(502, null)); targetServer.Start(); WebClient agent = new WebClient(); try { agent.DownloadData("http://localhost:80/bypass/"); } catch (WebException e) { Assert.AreEqual(HttpStatusCode.BadGateway, ((HttpWebResponse)e.Response).StatusCode); using (StreamReader r = new StreamReader(e.Response.GetResponseStream())) { string json = r.ReadToEnd(); Assert.AreEqual("", json); } return; } Assert.Fail("Expected exception is not thrown"); } }
public void TestByPass() { using (HttpServer proxyServer = new HttpServer(80)) using (HttpServer targetServer = new HttpServer(8080)) { MyForwardedHandler target = new MyForwardedHandler(); targetServer.AddHandler("/target/", target); targetServer.Start(); proxyServer.AddHandler("/", new MyHandler("21212")); // dummy proxyServer.AddDefaultHandler(new BypassHttpHandler("http://localhost:8080")); proxyServer.Start(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create( "http://localhost:80/target/?abc=123&qaz=wsx"); request.ContentType = "application/shawn"; request.Method = "POST"; request.ContentLength = 20; request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(new Cookie("cookie1", "val1", "", "localhost")); request.CookieContainer.Add(new Cookie("cookie2", "val2", "", "localhost")); using (StreamWriter w = new StreamWriter(request.GetRequestStream())) { w.Write("1234567890qwertyuiop"); } HttpWebResponse resp = (HttpWebResponse)request.GetResponse(); Assert.AreEqual(request.ContentType, MyForwardedHandler.recvContentType); Assert.AreEqual(request.ContentLength, MyForwardedHandler.recvContentLength); Assert.AreEqual(request.Method, MyForwardedHandler.recvMethod); Assert.AreEqual("val1", MyForwardedHandler.recvCookies["cookie1"].Value); Assert.AreEqual("val2", MyForwardedHandler.recvCookies["cookie2"].Value); Assert.AreEqual(2, MyForwardedHandler.recvCookies.Count); Assert.AreEqual("123", MyForwardedHandler.recvQueryString["abc"]); Assert.AreEqual("wsx", MyForwardedHandler.recvQueryString["qaz"]); Assert.AreEqual("/target/", MyForwardedHandler.recvPath); Assert.AreEqual("1234567890qwertyuiop", MyForwardedHandler.recvData); using (StreamReader r = new StreamReader(resp.GetResponseStream())) { Assert.AreEqual("write from target", r.ReadToEnd()); } Assert.AreEqual(2, resp.Cookies.Count); Assert.AreEqual("111", resp.Cookies["aaa"].Value); Assert.AreEqual("/target/", resp.Cookies["aaa"].Path); Assert.AreEqual("222", resp.Cookies["bbb"].Value); } }
public void TestDefaultHandler() { using (HttpServer server = new HttpServer(80)) { string reply1 = "from handler1"; server.AddDefaultHandler(new MyHandler("1234567890")); server.AddHandler("/class1/action1/", new MyHandler(reply1)); server.Start(); WebClient agent = new WebClient(); string replyFromHandler1 = agent.DownloadString( "http://127.0.0.1:80/class1/action1/pp/"); Assert.AreEqual(replyFromHandler1, "1234567890"); } }
protected override void OnStart(string[] args) { try { logger.Info("============== Starting Waveface Station ================="); ConfigThreadPool(); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Environment.CurrentDirectory = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location); logger.Debug("Initialize Waveface Service"); InitStationId(); InitResourceBasePath(); fastJSON.JSON.Instance.UseUTCDateTime = true; functionServer = new HttpServer(9981); // TODO: remove hard code stationTimer = new StationTimer(functionServer); logger.Debug("Add cloud forwarders to function server"); BypassHttpHandler cloudForwarder = new BypassHttpHandler(CloudServer.BaseUrl); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/auth/"); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/users/"); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/groups/"); cloudForwarder.AddExceptPrefix("/" + CloudServer.DEF_BASE_PATH + "/stations/"); functionServer.AddDefaultHandler(cloudForwarder); logger.Debug("Add handlers to function server"); functionServer.AddHandler("/", new DummyHandler()); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/view/", new AttachmentViewHandler(stationId)); AttachmentUploadHandler attachmentHandler = new AttachmentUploadHandler(); AttachmentUploadMonitor attachmentMonitor = new AttachmentUploadMonitor(); ImagePostProcessing imgProc = new ImagePostProcessing(); imgProc.ThumbnailUpstreamed += attachmentMonitor.OnThumbnailUpstreamed; attachmentHandler.ImageAttachmentSaved += imgProc.HandleImageAttachmentSaved; attachmentHandler.ImageAttachmentCompleted += imgProc.HandleImageAttachmentCompleted; attachmentHandler.ThumbnailUpstreamed += attachmentMonitor.OnThumbnailUpstreamed; CloudStorageSync cloudSync = new CloudStorageSync(); attachmentHandler.AttachmentSaved += cloudSync.HandleAttachmentSaved; attachmentHandler.ProcessSucceeded += attachmentMonitor.OnProcessSucceeded; functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/upload/", attachmentHandler); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/resourceDir/get/", new ResouceDirGetHandler(resourceBasePath)); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/resourceDir/set/", new ResouceDirSetHandler()); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/attachments/get/", new AttachmentGetHandler()); functionServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/availability/ping/", new PingHandler()); //if (Wammer.Utility.AutoRun.Exists("WavefaceStation")) //{ logger.Debug("Start function server"); functionServer.Start(); stationTimer.Start(); //} logger.Debug("Add handlers to management server"); managementServer = new HttpServer(9989); AddDriverHandler addDriverHandler = new AddDriverHandler(stationId, resourceBasePath); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/online/", new StationOnlineHandler(functionServer, stationTimer)); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/offline/", new StationOfflineHandler(functionServer, stationTimer)); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/drivers/add/", addDriverHandler); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/drivers/list/", new ListDriverHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/drivers/remove/", new RemoveOwnerHandler(stationId)); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/station/status/get/", new StatusGetHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/list", new ListCloudStorageHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/oauth/", new DropBoxOAuthHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/connect/", new DropBoxConnectHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/update/", new DropBoxUpdateHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/cloudstorage/dropbox/disconnect/", new DropboxDisconnectHandler()); managementServer.AddHandler("/" + CloudServer.DEF_BASE_PATH + "/availability/ping/", new PingHandler()); addDriverHandler.DriverAdded += new EventHandler <DriverAddedEvtArgs>(addDriverHandler_DriverAdded); logger.Debug("Start management server"); managementServer.Start(); logger.Info("Waveface station is started"); } catch (Exception ex) { logger.Error("Unknown exception", ex); throw; } }