Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / web / http.js
blobc8f98f85c72d847ca4bd9122f3ae87b90718a765
1 'use strict';
3 var http = require('http');
4 var useragent = require('useragent');
6 var ServerResponsePrototype = http.ServerResponse.prototype;
7 var IncomingMessagePrototype = http.IncomingMessage.prototype;
9 /* Monkey patch onto the response */
11 * This is needed as connect/express attempt to be too smart for their own good behind proxies (like NGINX) and convert relative URLS into absolute URLS. Unfortunately behind a proxy the URL will proably we wrong.
12 * TODO: raise this as a bug in express
14 ServerResponsePrototype.relativeRedirect = function(status, url) {
15 var req = this.req;
16 var body;
18 if (!url) {
19 url = status;
20 status = 302;
23 // Support text/{plain,html} by default
24 if (req.accepts('html')) {
25 body =
26 '<p>' +
27 http.STATUS_CODES[status] +
28 '. Redirecting to <a href="' +
29 url +
30 '">' +
31 url +
32 '</a></p>';
33 this.header('Content-Type', 'text/html');
34 } else {
35 body = http.STATUS_CODES[status] + '. Redirecting to ' + url;
36 this.header('Content-Type', 'text/plain');
39 // Respond
40 this.statusCode = status;
41 this.header('Location', url);
42 this.end(body);
45 IncomingMessagePrototype.getParsedUserAgent = function() {
46 if (this._parsedUserAgent) {
47 return this._parsedUserAgent;
50 var parsedUserAgent = useragent.parse(this.headers['user-agent']);
51 this._parsedUserAgent = parsedUserAgent;
52 return parsedUserAgent;