After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 694300 - Fix http-server.js example
Fix http-server.js example
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2013-02-20 18:42 UTC by Dan Winship
Modified: 2013-02-20 20:17 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
examples: fix http-server.js example (5.12 KB, patch)
2013-02-20 18:42 UTC, Dan Winship
reviewed Details | Review
examples: fix http-server.js example (5.29 KB, patch)
2013-02-20 18:53 UTC, Dan Winship
none Details | Review
examples: fix http-server.js example (5.24 KB, patch)
2013-02-20 20:15 UTC, Dan Winship
committed Details | Review

Description Dan Winship 2013-02-20 18:42:23 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.
Comment 1 Dan Winship 2013-02-20 18:42:24 UTC
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.
Comment 2 Jasper St. Pierre (not reading bugmail) 2013-02-20 18:47:17 UTC
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?
Comment 3 Dan Winship 2013-02-20 18:53:51 UTC
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
Comment 4 Dan Winship 2013-02-20 19:00:55 UTC
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...)
Comment 5 Jasper St. Pierre (not reading bugmail) 2013-02-20 19:05:55 UTC
(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".
Comment 6 Dan Winship 2013-02-20 20:15:15 UTC
Created attachment 236977 [details] [review]
examples: fix http-server.js example

indeed, with gjs from jhbuild \u263A works fine
Comment 7 Jasper St. Pierre (not reading bugmail) 2013-02-20 20:16:36 UTC
Review of attachment 236977 [details] [review]:

OK.
Comment 8 Dan Winship 2013-02-20 20:17:07 UTC
Attachment 236977 [details] pushed as 239b0c9 - examples: fix http-server.js example