GNOME Bugzilla – Bug 694300
Fix http-server.js example
Last modified: 2013-02-20 20:17:08 UTC
This might be a good example of doing random stuff with JavaScript, but it's an awful example of using SoupServer. The UTF-8-via-bytearray was because I couldn't remember how to make gjs convert the string to UTF-8 (soup_message_body_append() takes guchar*, not utf8). But it also serves to point out that you can pass binary data there. Shrug.
Created attachment 236970 [details] [review] 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.
Review of attachment 236970 [details] [review]: ::: examples/http-server.js @@ +5,1 @@ let main = function() { ugh, this isn't your fault, but this really should be "function main() {"... @@ +13,3 @@ + msg.response_headers.set_content_type('text/html', { charset: 'UTF-8' }); + msg.response_body.append('<html><body>Hello, ' + client.get_host() + '! '); + msg.response_body.append([ 0xE2, 0x98, 0xBA ]); You should be able to do "\u263A" here... @@ +17,3 @@ + }; + + let server = new Soup.Server({ port: 1080 }); Indentation is bad?
Created attachment 236971 [details] [review] examples: fix http-server.js example add more features. also fix the last remaining 3-space indents from the original
oops, attached that before I saw your review (In reply to comment #2) > let main = function() { > > ugh, this isn't your fault, but this really should be "function main() {"... fixed locally > + msg.response_body.append([ 0xE2, 0x98, 0xBA ]); > > You should be able to do "\u263A" here... You'd think, but it turns into ":" (aka 0x3A). I guess the string-to-guchar* conversion just strips off the high byte of each UCS-2 character? Is there some way to convert it manually before passing it to soup_message_body_append()? (And what do you think would be the best approach for an example program to be using? Or we could just punt and not have any UTF-8 in the response...)
(In reply to comment #4) > You'd think, but it turns into ":" (aka 0x3A). I guess the string-to-guchar* > conversion just strips off the high byte of each UCS-2 character? Is there some > way to convert it manually before passing it to soup_message_body_append()? > (And what do you think would be the best approach for an example program to be > using? Or we could just punt and not have any UTF-8 in the response...) Hm, do you have an up-to-date gjs and gobject-introspection clone? I thought we fixed that so that Unicode strings would be converted to UTF-8 if somebody asked for "binary data".
Created attachment 236977 [details] [review] examples: fix http-server.js example indeed, with gjs from jhbuild \u263A works fine
Review of attachment 236977 [details] [review]: OK.
Attachment 236977 [details] pushed as 239b0c9 - examples: fix http-server.js example