Permalink
Browse files

Latest SS.dll's with all StarterTemplate Integration tests passing

  • Loading branch information...
1 parent bd4f7a6 commit 64229255cf7d647a8a7303b67a3a797a35e95032 @mythz mythz committed Mar 3, 2011
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -7,17 +7,38 @@ namespace ServiceStack.ServiceClient.Web
{
public static class WebRequestExtensions
{
- public static string DownloadString(string url)
+ public static string DownloadUrl(this string url)
{
var webReq = WebRequest.Create(url);
using (var webRes = webReq.GetResponse())
+ return DownloadText(webRes);
+ }
+
+ public static string DownloadText(this WebResponse webRes)
+ {
using (var stream = webRes.GetResponseStream())
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
+ public static HttpWebResponse GetErrorResponse(this string url)
+ {
+ try
+ {
+ var webReq = WebRequest.Create(url);
+ var webRes = webReq.GetResponse();
+ var strRes = webRes.DownloadText();
+ Console.WriteLine("Expected error, got: " + strRes);
+ return null;
+ }
+ catch (WebException webEx)
+ {
+ return (HttpWebResponse) webEx.Response;
+ }
+ }
+
public static WebResponse UploadFile(this WebRequest webRequest,
FileInfo uploadFileInfo, string uploadFileMimeType)
{
@@ -1,4 +1,5 @@
using System;
+using System.IO;
using System.Net;
using System.Web;
using ServiceStack.Common;
@@ -99,17 +100,25 @@ public static string GetUrlHostName(this IHttpRequest httpReq)
return hostName;
}
- public static string GetFilePath(this IHttpRequest httpRequest)
+ public static string GetPhysicalPath(this IHttpRequest httpReq)
{
- var aspNetReq = httpRequest as HttpRequestWrapper;
+ var aspNetReq = httpReq as HttpRequestWrapper;
if (aspNetReq != null)
{
- return aspNetReq.Request.FilePath;
+ return aspNetReq.Request.PhysicalPath;
}
- var filePath = httpRequest.ApplicationFilePath.CombineWith(httpRequest.PathInfo);
+ var filePath = httpReq.ApplicationFilePath.CombineWith(httpReq.PathInfo);
return filePath;
}
+ public static string GetApplicationUrl(this HttpRequest httpReq)
+ {
+ var appPath = httpReq.ApplicationPath;
+ var baseUrl = httpReq.Url.Scheme + "://" + httpReq.Url.Host;
+ if (httpReq.Url.Port != 80) baseUrl += ":" + httpReq.Url.Port;
+ var appUrl = baseUrl.CombineWith(appPath);
+ return appUrl;
+ }
}
}
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;
-using System.Net;
using System.Web;
+using ServiceStack.Common;
using ServiceStack.Common.Utils;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
@@ -17,9 +17,11 @@ public class ServiceStackHttpHandlerFactory
static readonly List<string> WebHostRootFileNames = new List<string>();
static private readonly string WebHostPhysicalPath = null;
static private readonly string DefaultRootFileName = null;
+ static private string ApplicationBaseUrl = null;
static private readonly IHttpHandler DefaultHttpHandler = null;
static private readonly IHttpHandler ForbiddenHttpHandler = null;
static private readonly IHttpHandler NotFoundHttpHandler = null;
+ static private readonly IHttpHandler StaticFileHandler = new StaticFileHandler();
private static readonly bool IsIntegratedPipeline = false;
static ServiceStackHttpHandlerFactory()
@@ -31,32 +33,36 @@ static ServiceStackHttpHandlerFactory()
IsIntegratedPipeline = (bool)pi.GetGetMethod().Invoke(null, new object[0]);
}
- //DefaultHttpHandler not supported in IntegratedPipeline mode
- if (!IsIntegratedPipeline)
- DefaultHttpHandler = new DefaultHttpHandler();
-
ForbiddenHttpHandler = new ForbiddenHttpHandler();
var isAspNetHost = HttpListenerBase.Instance == null || HttpContext.Current != null;
WebHostPhysicalPath = isAspNetHost
? "~".MapHostAbsolutePath()
: "~".MapAbsolutePath();
- foreach (var fileName in Directory.GetFiles(WebHostPhysicalPath))
+ //DefaultHttpHandler not supported in IntegratedPipeline mode
+ if (!IsIntegratedPipeline && isAspNetHost)
+ DefaultHttpHandler = new DefaultHttpHandler();
+
+ var hostedAtRootPath = EndpointHost.Config.ServiceStackHandlerFactoryPath == null;
+ if (hostedAtRootPath)
{
- var fileNameLower = Path.GetFileName(fileName).ToLower();
- if (DefaultRootFileName == null && EndpointHost.Config.DefaultDocuments.Contains(fileNameLower))
+ foreach (var fileName in Directory.GetFiles(WebHostPhysicalPath))
{
- DefaultRootFileName = fileNameLower;
- if (DefaultHttpHandler == null)
- DefaultHttpHandler = new RedirectHttpHandler { RelativeUrl = DefaultRootFileName };
+ var fileNameLower = Path.GetFileName(fileName).ToLower();
+ if (DefaultRootFileName == null && EndpointHost.Config.DefaultDocuments.Contains(fileNameLower))
+ {
+ DefaultRootFileName = fileNameLower;
+ if (DefaultHttpHandler == null)
+ DefaultHttpHandler = new RedirectHttpHandler { RelativeUrl = DefaultRootFileName };
+ }
+ WebHostRootFileNames.Add(Path.GetFileName(fileNameLower));
+ }
+ foreach (var dirName in Directory.GetDirectories(WebHostPhysicalPath))
+ {
+ var dirNameLower = Path.GetFileName(dirName).ToLower();
+ WebHostRootFileNames.Add(Path.GetFileName(dirNameLower));
}
- WebHostRootFileNames.Add(Path.GetFileName(fileNameLower));
- }
- foreach (var dirName in Directory.GetDirectories(WebHostPhysicalPath))
- {
- var dirNameLower = Path.GetFileName(dirName).ToLower();
- WebHostRootFileNames.Add(Path.GetFileName(dirNameLower));
}
NotFoundHttpHandler = string.IsNullOrEmpty(EndpointHost.Config.NotFoundRedirectPath)
@@ -83,7 +89,21 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
var pathInfo = context.Request.GetPathInfo();
if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
+ {
+ if (ApplicationBaseUrl == null)
+ {
+ ApplicationBaseUrl = context.Request.GetApplicationUrl();
+
+ var defaultRedirectUrl = DefaultHttpHandler as RedirectHttpHandler;
+ if (defaultRedirectUrl != null && defaultRedirectUrl.AbsoluteUrl == null)
+ {
+ defaultRedirectUrl.AbsoluteUrl = ApplicationBaseUrl.CombineWith(
+ defaultRedirectUrl.RelativeUrl);
+ }
+ }
+
return DefaultHttpHandler;
+ }
if (mode != null && pathInfo.EndsWith(mode))
{
@@ -103,7 +123,7 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
return okToServe ? DefaultHttpHandler : ForbiddenHttpHandler;
}
- return GetHandlerForPathInfo(context.Request.HttpMethod, pathInfo)
+ return GetHandlerForPathInfo(context.Request.HttpMethod, pathInfo, context.Request.FilePath)
?? NotFoundHttpHandler;
}
@@ -127,18 +147,18 @@ public static IHttpHandler GetHandler(IHttpRequest httpReq)
|| requestPath == mode + "/")
{
//TODO: write test for this
- if (httpReq.GetFilePath() != WebHostPhysicalPath
+ if (httpReq.GetPhysicalPath() != WebHostPhysicalPath
|| !File.Exists(Path.Combine(httpReq.ApplicationFilePath, DefaultRootFileName ?? "")))
{
return new IndexPageHttpHandler();
}
}
- var okToServe = ShouldAllow(httpReq.GetFilePath());
+ var okToServe = ShouldAllow(httpReq.GetPhysicalPath());
return okToServe ? DefaultHttpHandler : ForbiddenHttpHandler;
}
- return GetHandlerForPathInfo(httpReq.HttpMethod, pathInfo)
+ return GetHandlerForPathInfo(httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath())
?? NotFoundHttpHandler;
}
@@ -193,7 +213,7 @@ private static bool ShouldAllow(string filePath)
return EndpointHost.Config.AllowFileExtensions.Contains(fileExt.Substring(1));
}
- public static IHttpHandler GetHandlerForPathInfo(string httpMethod, string pathInfo)
+ public static IHttpHandler GetHandlerForPathInfo(string httpMethod, string pathInfo, string filePath)
{
var pathParts = pathInfo.TrimStart('/').Split('/');
if (pathParts.Length == 0) return new NotFoundHttpHandler();
@@ -204,9 +224,7 @@ public static IHttpHandler GetHandlerForPathInfo(string httpMethod, string pathI
var existingFile = pathParts[0].ToLower();
if (WebHostRootFileNames.Contains(existingFile))
{
- //Avoid recursive redirections
- //return !IsIntegratedPipeline ? DefaultHttpHandler : new StaticFileHandler();
- return new StaticFileHandler();
+ return ShouldAllow(filePath) ? StaticFileHandler : ForbiddenHttpHandler;
}
var restPath = RestHandler.FindMatchingRestPath(httpMethod, pathInfo);
@@ -25,16 +25,16 @@ public void ProcessRequest(IHttpRequest request, IHttpResponse response, string
if (string.IsNullOrEmpty(RelativeUrl) && string.IsNullOrEmpty(AbsoluteUrl))
throw new ArgumentNullException("RelativeUrl or AbsoluteUrl");
- if (!string.IsNullOrEmpty(RelativeUrl))
+ if (!string.IsNullOrEmpty(AbsoluteUrl))
{
- var absoluteUrl = request.AbsoluteUri.WithTrailingSlash() + this.RelativeUrl;
response.StatusCode = (int)HttpStatusCode.Redirect;
- response.AddHeader(HttpHeaders.Location, absoluteUrl);
+ response.AddHeader(HttpHeaders.Location, this.AbsoluteUrl);
}
else
{
+ var absoluteUrl = request.AbsoluteUri.WithTrailingSlash() + this.RelativeUrl;
response.StatusCode = (int)HttpStatusCode.Redirect;
- response.AddHeader(HttpHeaders.Location, this.AbsoluteUrl);
+ response.AddHeader(HttpHeaders.Location, absoluteUrl);
}
response.Close();
}
@@ -51,17 +51,18 @@ public void ProcessRequest(HttpContext context)
if (string.IsNullOrEmpty(RelativeUrl) && string.IsNullOrEmpty(AbsoluteUrl))
throw new ArgumentNullException("RelativeUrl or AbsoluteUrl");
- if (!string.IsNullOrEmpty(RelativeUrl))
+ if (!string.IsNullOrEmpty(AbsoluteUrl))
{
- var absoluteUrl = request.ApplicationPath.WithTrailingSlash() + this.RelativeUrl;
response.StatusCode = (int)HttpStatusCode.Redirect;
- response.AddHeader(HttpHeaders.Location, absoluteUrl);
+ response.AddHeader(HttpHeaders.Location, this.AbsoluteUrl);
}
else
{
+ var absoluteUrl = request.Url.AbsoluteUri.WithTrailingSlash() + this.RelativeUrl;
response.StatusCode = (int)HttpStatusCode.Redirect;
- response.AddHeader(HttpHeaders.Location, this.AbsoluteUrl);
+ response.AddHeader(HttpHeaders.Location, absoluteUrl);
}
+ response.Flush();
response.Close();
}
@@ -50,7 +50,7 @@ public void ProcessRequest(HttpContext context)
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName)
{
- var fileName = request.GetFilePath();
+ var fileName = request.GetPhysicalPath();
var fi = new FileInfo(fileName);
if (!fi.Exists)
@@ -43,25 +43,29 @@ public void OnTestFixtureTearDown()
[Test]
public void Root_path_redirects_to_metadata_page()
{
- var webReq = (HttpWebRequest)WebRequest.Create(ListeningOn);
-
- var webRes = (HttpWebResponse)webReq.GetResponse();
-
- var html = new StreamReader(webRes.GetResponseStream()).ReadToEnd();
-
+ var html = ListeningOn.DownloadUrl();
Assert.That(html.Contains("The following operations are supported."));
}
[Test]
- public void Can_download_default_html_page()
+ public void Can_download_webpage_html_page()
{
- var webReq = (HttpWebRequest)WebRequest.Create(ListeningOn + "default.html");
-
- var webRes = (HttpWebResponse)webReq.GetResponse();
+ var html = (ListeningOn + "webpage.html").DownloadUrl();
+ Assert.That(html.Contains("Default index ServiceStack.WebHost.Endpoints.Tests page"));
+ }
- var html = new StreamReader(webRes.GetResponseStream()).ReadToEnd();
+ [Test]
+ public void Gets_404_on_non_existant_page()
+ {
+ var webRes = (ListeningOn + "nonexistant.html").GetErrorResponse();
+ Assert.That(webRes.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
+ }
- Assert.That(html.Contains("Default index ServiceStack.WebHost.Endpoints.Tests page"));
+ [Test]
+ public void Gets_403_on_page_with_non_whitelisted_extension()
+ {
+ var webRes = (ListeningOn + "webpage.forbidden").GetErrorResponse();
+ Assert.That(webRes.StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
}
[Test]
@@ -86,12 +90,13 @@ public void Can_call_jsv_debug_on_GetFactorial_WebService()
}
[Test]
- public void Calling_missing_web_service_does_not_break_HttpListener()
+ public void Calling_missing_web_service_does_not_break_HttpListener()
{
+ var missingUrl = ListeningOn + "missing.html";
int errorCount = 0;
try
{
- var call1 = new StreamReader(WebRequest.Create(ListeningOn).GetResponse().GetResponseStream()).ReadToEnd();
+ var call1 = new StreamReader(WebRequest.Create(missingUrl).GetResponse().GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
@@ -100,7 +105,7 @@ public void Calling_missing_web_service_does_not_break_HttpListener()
}
try
{
- var call2 = new StreamReader(WebRequest.Create(ListeningOn).GetResponse().GetResponseStream()).ReadToEnd();
+ var call2 = new StreamReader(WebRequest.Create(missingUrl).GetResponse().GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
@@ -219,7 +219,8 @@
<Link>sqlite3.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="default.html" />
+ <None Include="webpage.forbidden" />
+ <Content Include="webpage.html" />
<Content Include="TestExistingDir\default.html" />
<Content Include="TestExistingDir\upload.html" />
</ItemGroup>
@@ -39,7 +39,7 @@ public void Resolves_the_right_handler_for_expexted_paths()
foreach (var item in pathInfoMap)
{
var expectedType = item.Value;
- var handler = ServiceStackHttpHandlerFactory.GetHandlerForPathInfo(null, item.Key);
+ var handler = ServiceStackHttpHandlerFactory.GetHandlerForPathInfo(null, item.Key, null);
Assert.That(handler.GetType(), Is.EqualTo(expectedType));
}
}
@@ -51,7 +51,7 @@ public void Resolves_the_right_handler_for_case_insensitive_expexted_paths()
{
var expectedType = item.Value;
var lowerPathInfo = item.Key.ToLower();
- var handler = ServiceStackHttpHandlerFactory.GetHandlerForPathInfo(null, lowerPathInfo);
+ var handler = ServiceStackHttpHandlerFactory.GetHandlerForPathInfo(null, lowerPathInfo, null);
Assert.That(handler.GetType(), Is.EqualTo(expectedType));
}
}
@@ -0,0 +1 @@
+This page should not be displayed.
@@ -111,10 +111,12 @@
<Content Include="TestExistingDir\default.html" />
<Content Include="TestExistingDir\upload.html" />
<Content Include="Web.config" />
+ <Content Include="webpage.html" />
</ItemGroup>
<ItemGroup>
<Compile Include="Services\CachedMoviesService.cs" />
<Compile Include="Services\CustomFormDataService.cs" />
+ <Compile Include="Tests\AppHostBaseTests.cs" />
<Compile Include="Tests\CachedServiceTests.cs" />
<Compile Include="Tests\CsvContentTypeFilterTests.cs" />
<Compile Include="Global.asax.cs">
@@ -168,6 +170,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Properties\DataSources\ServiceStack.ServiceInterface.ServiceModel.ResponseStatus.datasource" />
+ <None Include="webpage.forbidden" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
Oops, something went wrong.

0 comments on commit 6422925

Please sign in to comment.