diff options
author | Dan Winship <danw@gnome.org> | 2013-02-20 13:22:20 -0500 |
---|---|---|
committer | Dan Winship <danw@gnome.org> | 2013-02-20 15:14:09 -0500 |
commit | 239b0c9230a33b53884e11a370f9a6ad4169645c (patch) | |
tree | f60737013631aca9fe43c1372fcfd395817012cf /examples/http-server.js | |
parent | 8d9d81e5e4efcf96d2bd06712a429240916a3d4f (diff) | |
download | gjs-239b0c9230a33b53884e11a370f9a6ad4169645c.tar.gz |
examples: fix http-server.js example
For some reason, this example was reimplementing a bunch of
SoupServer's functionality itself in JavaScript, rather than using
SoupServer in the expected way. (This may be because either gi or
Soup-2.4.gir was not up to the task at the time the example was
written.)
Anyway, fix. Also, spice it up a bit.
https://bugzilla.gnome.org/show_bug.cgi?id=694300
Diffstat (limited to 'examples/http-server.js')
-rw-r--r-- | examples/http-server.js | 147 |
1 files changed, 21 insertions, 126 deletions
diff --git a/examples/http-server.js b/examples/http-server.js index 9a180e1f..36c73f02 100644 --- a/examples/http-server.js +++ b/examples/http-server.js @@ -1,133 +1,28 @@ // 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(); +function main() { + let handler = function(server, msg, path, query, client) { + msg.status_code = 200; + msg.response_headers.set_content_type('text/html', {}); + msg.response_body.append('<html><body>Greetings, visitor from ' + client.get_host() + '<br>What is your name?<form action="/hello"><input name="myname"></form></body></html>\n'); + }; + let helloHandler = function(server, msg, path, query, client) { + if (!query) { + msg.set_redirect(302, '/'); + return; + } + + msg.status_code = 200; + msg.response_headers.set_content_type('text/html', { charset: 'UTF-8' }); + msg.response_body.append('<html><body>Hello, ' + query.myname + '! \u263A<br><a href="/">Go back</a></body></html>'); + }; + + let server = new Soup.Server({ port: 1080 }); + server.add_handler('/', handler); + server.add_handler('/hello', helloHandler); + server.run(); } main(); |