How do I serve static files from a different URL with Dart? - dart

With Dart, I've got awesome.html, but I'd like it to be /awesome. Is this purely an .htaccess (I'm using Apache) thing, or is there a way to go about this the Dart or "modern web development" way?
This .htaccess bit directs /awesome to /awesome.html:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
But then all my relative URL references (to css/js/images) break, and if I rewrite them from "assets/whatever" to "/assets/whatever" it'll break when working in the Dart Editor since it uses URLs like:
http://127.0.0.1:3030/Users/dave/Sites/my-dart-app/web/awesome.html
Ideas? Best practices? Thank you!

thanks for the question!
The answer depends on if you have a proxy or web server in front of your Dart server VM. If you have a proxy in front, then the proxy can do the URL rewriting before the request hits your Dart VM. This is a nice scenario anyway, because a proxy can do caching, SSL, load balancing, and more. The Dart VM is then just an "app server" in this scenario. I would recommend placing an industrial strength web server or proxy in front just as a best practice.
However, if you want to do URL masking and rewriting purely in Dart, here is some code. As Kai says in the comments above, this is generally a framework's job. But I'll include some code here anyway for fun. :)
import 'dart:io';
import 'dart:json';
class StaticFileHandler {
final String basePath;
StaticFileHandler(this.basePath);
_send404(HttpResponse response) {
response.statusCode = HttpStatus.NOT_FOUND;
response.outputStream.close();
}
String rewritePath(String path) {
String newPath = path;
if (path == '/' || path.endsWith('/')) {
newPath = '${path}index.html';
} else if (!path.endsWith('.html')) {
newPath = "${path}.html";
}
return newPath;
}
// TODO: etags, last-modified-since support
onRequest(HttpRequest request, HttpResponse response) {
String path = rewritePath(request.path);
final File file = new File('${basePath}${path}');
file.exists().then((found) {
if (found) {
file.fullPath().then((String fullPath) {
if (!fullPath.startsWith(basePath)) {
_send404(response);
} else {
file.openInputStream().pipe(response.outputStream);
}
});
} else {
_send404(response);
}
});
}
}
runServer(String basePath, int port) {
HttpServer server = new HttpServer();
server.defaultRequestHandler = new StaticFileHandler(basePath).onRequest;
server.onError = (error) => print(error);
server.listen('127.0.0.1', 1337);
print('listening for connections on $port');
}
main() {
var script = new File(new Options().script);
var directory = script.directorySync();
runServer("${directory.path}", 1337);
}

By the way, I've updated the rewritePath() function in Seth's code some so that it doesn't rewrite assets like .dart and .css files to .html, and so that it works w/ my client-side stuff living in /web.
String rewritePath(String path) {
String newPath = path;
if (path == '/' || path.endsWith('/')) {
newPath = '/web${path}index.html';
} else if (!path.endsWith('.html')) {
if (path.contains('.')) {
newPath = '/web${path}';
} else {
newPath = '/web${path}.html';
}
} else {
newPath = '/web${path}.html';
}
//peek into how it's rewriting the paths
print('$path -> $newPath');
return newPath;
}
It's of course super basic, and a framework that handles routing would certainly come in handy (would love to see what you're building #Kai).

Related

Varnish direct request to internal ip

I have an hash configuration like this
sub vcl_init {
new bar = directors.hash();
bar.add_backend(server1, 1.0);
bar.add_backend(server2, 1.0);
}
I would like to add a rewrite rule that if in the url there is a word it must redirect to one specific internal server
if (req.url ~ "/newsletter" ) {
set req.http.x-redir = "https://" + "10.1.3.4" + req.url;
return (synth (301, req.http.x-redir));
}
But this rule doesn't work because 10.1.3.4 is an internal ip not a DNS. Is it possible to do this?
Seems like you want to use a different director backend (the internal server) if the request contains newsletter. Thats possible.
Example
sub vcl_recv {
if (req.url ~ "^/newsletter") {
set req.backend = newsletter;
} else {
set req.backend = default.
}
}
See https://varnish-cache.org/docs/3.0/tutorial/advanced_backend_servers.html
But you cant do a 301, thats for the external request, while here we are talking internal routing.

How does spray find resources - e.g. javascript

It was simple to build my first servlet with spray-io.
But the recources referenced in the header are never found.
< head>
...
< script src="javascript/jquery/jquery-1.9.1.js"/>
...
< / head>
In which directory does one have to put those recsources, or how can spray be directed to look up there?
Simple question, but I could not figure out.
Many thankx
Girgl
With Spray routing, I use these directives -
pathPrefix("css") { get { getFromResourceDirectory("css") } } ~
pathPrefix("js") { get { getFromResourceDirectory("js") } } ~
"css" and "js" have to be in src/main/resources directory
If you are using spray routing, then it should be easy, just provide a route for your static resources. For example you can do the following:
Let's say that your static resources are in /css, /js and /img folders:
def staticPrefixes = List("css", "js", "img") map { pathPrefix(_) } reduce { _ | _ }
with pathPrefix you are making each path a prefix of an unmatched path. Then you need a directive to extract path to static file from the request, for example you can do it like this:
def stripLeadingSlash(path: String) = if (path startsWith "/") path.tail else path
val staticPath =
staticPrefixes &
cache(routeCache()) &
extract(ctx ⇒ stripLeadingSlash(ctx.request.uri.path.toString))
then construct your route which would manage your resources:
val staticRoutes =
get {
staticPath { path ⇒
getFromResource(path.toString)
}
}

Legacy PHP urls do not invoke custom routing in MVC

We recently replaced an old php website with an asp.net MVC website. In order to prevent 404 errors from the legacy urls in search engines, we setup a custom legacy route system via - http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC
The code works on my local machine, and it redirects to the correct route; however, the live server issues a 404. Bonus problem/clue, the 404 is not our custom 404 page but the iis 6 default page.
The code:
public class LegacyUrlRoute: RouteBase
{
// source: http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC
public override RouteData GetRouteData(HttpContextBase httpContext)
{
const string status = "301 Moved Permanently";
var request = httpContext.Request;
var response = httpContext.Response;
var legacyUrl = request.Url.ToString();
var newUrl = "";
if (legacyUrl.Contains(".php"))
{
newUrl = "/";
if (legacyUrl.Contains("support/mailfilter.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/support/default.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/business/default.php"))
newUrl = "/services/";
else if (legacyUrl.Contains("/residential/default.php"))
newUrl = "/services/";
else if (legacyUrl.Contains("/about/default.php"))
newUrl = "/home/about/";
else if (legacyUrl.Contains("/jobs.php"))
newUrl = "/jobs/";
else if (legacyUrl.Contains("/support/links.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/support/settings.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/default.php"))
newUrl = "/";
response.Status = status;
response.RedirectLocation = newUrl;
response.End();
}
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}
Note:
We suspect that the problem is with iis being unable to serve php pages, but I can't seem to find a setting in iis to fix the problem. It is as if the request never hits the Controller code, error or otherwise. All other pages/routing is working perfectly.
Found the answer.
The problem is due to the fact that iis never starts the asp.net service if the extension is .php.
The solution is to go to Properties>Home Directory>Configuration, find the .php extension, change the executable path to the asp.net path, and limit it to "GET,HEAD,POST,DEBUG" or whatever you prefer. I originally selected the "All Verbs" radio button, but that did not work.
Though the page did not specifically have the answer I came up with, this page did help.

get absolute url on handle ressource request on blackberry

I use this method the get the urls of ressources contain on web page
public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception
{
final String url = request.getURL();
return super.handleResourceRequest(request);
}
But, I made request.getURL(); it returns relative url and not the absolute url.
How can I change it to get the absolute URL?
When I run your code, it does return me absolute URLs, even when my web page contained relative links. That said, it wouldn't surprise me if sometimes, it doesn't. I haven't fully tested this code, but I would think you could try something like this.
Basically, you check to see if the URL is absolute, and if not, you assemble an absolute URL by using the parent BrowserField document URL:
ProtocolController controller = new ProtocolController(_browserField) {
public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception {
String absoluteUrl = null;
URI uri = URI.create(request.getURL());
if (uri.isAbsolute()) {
absoluteUrl = request.getURL();
} else {
String docUrl = _browserField.getDocumentUrl();
String url = request.getURL();
if (url.startsWith("/")) {
// the URL is relative to the server root
URI docUri = URI.create(docUrl);
absoluteUrl = docUri.getScheme() + "://" + docUri.getHost() + url;
} else {
// the URL is relative to the document URL
absoluteUrl = docUrl + url;
}
}
System.out.println(" requesting: " + absoluteUrl);
return super.handleResourceRequest(request);
}
}
Again, for me, I was getting absolute URLs, so I couldn't easily test the code in the branch where the URL is relative. So, it's possible that I'm dropping a "/" somewhere, or not handling file:/// URLs properly.
But, this is a starting point, to workaround your problem.

URL Rewriter Managed Fusion strip a QueryString

I'm Using Managed Fusion URL Rewriter, so far it has been awesome.
Now I just need to make sure a query string is passed in a URL from a proxy website.
I Need to pass CODE=777 on every call so I have the code below:
RewriteRule ^(.*) http://www.somewebsite.com/$1?CODE=777[P, QSA,I,L]
However I need to strip the query string before hand. So I used the code below:
RewriteCond %{QUERY_STRING} ^CODE=([0-9]+)+$
RewriteRule ^(.*) http://www.somewebsite.com/$1? [P, QSA]
RewriteRule ^(.*) http://www.somewebsite.com/$1?CODE=777[P, QSA,I,L]
But it fails when I redirect with additional querystring such as below
http://proxyserver.com?othercode=something
Any Ideas how to strip a particular querystring without removing all query strings paramters?
I downloaded the source code from Managed Fusion and added the project to my solution.
I searched and found this bit of code below that appends the QueryString. I updated the code by adding a new method to remove the querystring CODE and affix a FIXED one.
I also added a Setting to control the FIXED Value assigned to the CODE.
private Uri AppendQueryString(Uri substituedUrl, Uri existingUrl)
{
string append = existingUrl.Query.TrimStart('?');
if (!String.IsNullOrEmpty(append))
{
UriBuilder builder = new UriBuilder(substituedUrl);
if (String.IsNullOrEmpty(builder.Query) || builder.Query == "?")
builder.Query = append;
else
builder.Query = builder.Query.TrimStart('?') + "&" + append;
return AppendFixedQueryString(builder.Uri, existingUrl);
}
return AppendFixedQueryString(substituedUrl, existingUrl);
}
ADDED NEW METHOD
private Uri AppendFixedQueryString(Uri substituedUrl, Uri existingUrl)
{
string append = string.Format("CODE={0}", Settings.Default.CODE);
if (!String.IsNullOrEmpty(append))
{
UriBuilder builder = new UriBuilder(substituedUrl);
builder.Query = Regex.Replace(builder.Query, #"CODE=([0-9]+)", String.Empty);
if (String.IsNullOrEmpty(builder.Query) || builder.Query == "?")
builder.Query = append;
else
builder.Query = builder.Query.TrimStart('?').Trim("&".ToCharArray()) + "&" + append;
return builder.Uri;
}
return substituedUrl;
}
With this modification. even if the user explictly types CODE=123 at the URL it will just be ignored and a fixed CODE value will be assigned

Resources