File: http-server.js

package info (click to toggle)
gjs 1.32.0-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,436 kB
  • sloc: ansic: 27,721; sh: 11,157; makefile: 203; python: 120; cpp: 49
file content (133 lines) | stat: -rw-r--r-- 3,472 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// This is a simple example of a HTTP server in Gjs using libsoup

const Lang = imports.lang;

const GLib = imports.gi.GLib;
const Soup = imports.gi.Soup;

function HTTPServer(args) {
  this._init(args);
}

HTTPServer.prototype = {
    _init : function(args) {
        this._handlers = [];
        this._port = 'port' in args ? args.port : 1080;
        this._server = this._startServer();
    },

    run : function() {
        this._server.run()
    },

    addHandler : function(path, handler) {
        this._handlers.push({ pathRegexp: new RegExp(path), handler : handler });
    },

    _startServer : function() {
        let server = new Soup.Server({ port: this._port});
        server.connect("request-started",
                       Lang.bind(this, this._onRequestStarted));
        server.connect("request-finished",
                       Lang.bind(this, this._onRequestFinished));
        return server;
    },

    _invokeHandlers : function(message) {
        let uri = message.uri;
        for (let i = 0; i < this._handlers.length; ++i) {
            let handlerGroup = this._handlers[i];
            if (uri.path.match(handlerGroup.pathRegexp)) {
                let request = new HTTPRequest({ uri: uri });
                let response = handlerGroup.handler(request);
                if (response !== undefined) {
                    this._setResponse(message, response);
                    return true;
                }
            }
        }
      return false;
    },

    _setResponse : function(message, response) {
        message.set_status(response.status);
        message.set_response(response.mimeType, Soup.MemoryUse.COPY,
                             response.content, response.content.length);
    },

   _onRequestStarted : function(server, message, context) {
        message._gotBodyId = message.connect("got-body",
                         Lang.bind(this, this._onMessageGotBody));
   },

    _onMessageGotBody : function(message) {
        if (!this._invokeHandlers(message)) {
            let error = new HTTPResponse("ERROR: Not found.");
            error.status = 404;
            this._setResponse(message, error);
        }
   },

   _onRequestFinished : function(server, message, context) {
        message.disconnect(message._gotBodyId);
   }
};

function HTTPRequest(args) {
   this._init(args);
};

HTTPRequest.prototype = {
   _init : function(args) {
      this._uri = args.uri;
   },

   toString : function() {
       return "[object HTTPRequest uri=" + this._uri.to_string(false) + "]";
   }

};

function HTTPResponse(content) {
   this._init(content);
};

HTTPResponse.prototype = {
   _init : function(content) {
      this._content = content
      this._status = 200;
      this._mimeType = "text/html";
   },

   toString : function() {
       return "[object HTTPResponse uri=" + this._content + "]";
   },

   get content() {
      return this._content;
   },

   get mimeType() {
      return this._mimeType;
   },

   set status(status) {
      this._status = status
   },

   get status() {
      return this._status;
   }
};

let main = function() {
   let handler = function(request) {
       return new HTTPResponse('Index page<br><a href="/hello">Say hi</a>\n', undefined, 200);
   };
   let server = new HTTPServer({ port: 1080 });
   server.addHandler("^/$", handler);
   server.addHandler("^/hello$", function() new HTTPResponse('Hello!<br><a href="/">Go back</a>'));
   server.run();
}

main();