playframework/Play20 — pull request #144 by julienrf (2012-02-26 14:39:04+00:00)
Title: Provide absoluteURL() and webSocketURL() functions to JavaScript reverse router

[#116](https://play.lighthouseapp.com/projects/82401/tickets/116-provide-websocketurl-and-absoluteurl-functions-in-the-javascript-reverse-router)
Allows to compute absolute and web socket urls from the JavaScript reverse router by providing two methods similar to the ones of the Scala [Call](https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/mvc/Http.scala#L154) API.

Example

```
GET /foo controllers.Application.foo()
```

```javascript
var route = javascriptRoutes.controllers.Application.foo();
route.url == '/foo';
route.absoluteURL() == 'http://yourdomain.com/foo';
route.webSocketURL() == 'ws://yourdomain.com/foo';
route.absoluteURL(true) == 'https://yourdomain.com/foo';
route.webSocketURL(true) == 'wss://yourdomain.com/foo';
// Alternative API
route.absoluteURL({secure: true}) == 'https://yourdomain.com/foo';
route.webSocketURL({secure: true}) == 'wss://yourdomain.com/foo';
```

**Required a change over the current API: `Routes.javascriptRouter` method takes an additional implicit parameter of type `play.api.mvc.RequestHeader`.**

This change is needed because the host name should not be resolved client side. Actually, computing the host name client side (using `window.location.host`) can raise issues in some specific cases.
For example, consider a service located at http://service.com, which provides a JavaScript entry point (e.g. http://service.com/api.js) allowing to retrieve the URL of some streaming content. To use it in your application located at http://application.com, you just write the following in your HTML files:

```html

```

If the streamingUrl was computed using `window.location.host` to resolve the host name, it would return “ws://application.com/streaming” instead of “ws://service.com/streaming”. Computing the host name from the server solves this problem.