CVu, Code Critique, and D

Anton Fediushin fediushin.anton at yandex.ru
Tue Jun 19 13:43:08 UTC 2018


On Tuesday, 19 June 2018 at 08:56:05 UTC, Russel Winder wrote:
> I believe we have a first, Code Critique 112 is a D code. And 
> indeed a vibe.d one. I believe a number of people from this 
> email list should volunteer themselves to do a constructive 
> critique to help educate ACCU members. With the obvious subtext 
> of D being a far, far better language than C++ ;-)

I'm not quite sure constructive critique is possible in this 
case. It's just a bad piece of code poorly implementing something 
that is a part of the vibe-core.

>
> Below is the email from accu-general
>
> -------------------------------------------------------------------
>
>
> Code Critique 112
>
> Hello all,
> Below is the code for Code Critique number 112
>
> As usual, please don't reply to the list with your entry but 
> email scc at accu.org for collation.
>
>
> ------------------------------------------------------------------------
> (Submissions to scc at accu.org by August 1st)
>
> Further to articles introducing D, I am attempting to use the 
> event-driven Vibe.d server library, which I understand to be 
> like a native version of Python's Tornado and potentially a 
> “killer app” of D as I haven't seen any other mature 
> event-driven server library in a compiled language.
>
> I would like to build a back-end server that performs some 
> processing on the body of the HTTP request it receives.  To 
> begin with, I would like it to simply echo the request body 
> back to the client.

There is vibe.web.rest which covers 90% of everything one might 
need for their back-end application. There's practically no 
reason to bother with bodyReaders/Writers.

> My code works but there are three problems: (1) when I issue a 
> POST request with lynx, 3 spaces are added to the start of the 
> response text, (2) I cannot test with nc because it just says 
> 404 and doesn't log anything, and (3) I'm worried that reading 
> and writing just one byte at a time is inefficient, but I can't 
> see how else to do it: I raised a "more documentation needed" 
> bug at https://github.com/vibe-d/vibe.d/issues/2139 but at the 
> time of writing nobody has replied (should I have used a 
> different forum?)
>
> --- code ---
> import vibe.d;
> void main() {
>     auto settings = new HTTPServerSettings;
>     settings.port = 8080;
>     listenHTTP(settings, (req, res) {
>         ubyte[1] s;
>         while(!req.bodyReader.empty()) {
>           req.bodyReader.read(s);
>           res.bodyWriter.write(s);
>         }
>       });
>     runApplication();
> }


1. It has something to do with lynx, curl works flawlessly
2. Writing HTTP manually is somewhat painful. Request should look 
something like this:

$ echo -ne "POST / HTTP/1.1\r\nHost: 
localhost:8080\r\nContent-Length: 3\r\n\r\nHey" | nc localhost 
8080

3. There's `pipe` function in `vibe.core.stream` which does 
exactly that. I haven't looked at its code but I'm pretty sure 
it's far more efficient than byte-by-byte approach:

```
import vibe.d;

void main() {
	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	listenHTTP(settings, (req, res) {
		req.bodyReader.pipe(res.bodyWriter);
	});
	runApplication();
}
```




More information about the Digitalmars-d mailing list