Update/fix example and remove log noise.

This commit is contained in:
Sönke Ludwig 2017-09-02 17:39:20 +02:00
parent c06e24494d
commit f9762c46e4
No known key found for this signature in database
GPG key ID: D95E8DB493EE314C

View file

@ -1,10 +1,11 @@
import vibe.core.core; import vibe.core.core;
import vibe.core.log; import vibe.core.log;
import vibe.core.net; import vibe.core.net;
//import vibe.stream.operations; import vibe.core.stream;
import std.exception : enforce; import std.exception : enforce;
import std.functional : toDelegate; import std.functional : toDelegate;
import std.range.primitives : isOutputRange;
void main() void main()
@ -13,19 +14,14 @@ void main()
nothrow @safe { nothrow @safe {
try { try {
while (!conn.empty) { while (!conn.empty) {
logInfo("read request");
while (true) { while (true) {
CountingRange r; CountingRange r;
conn.readLine(r); conn.readLine(r);
if (!r.count) break; if (!r.count) break;
} }
logInfo("write answer"); conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\n\r\nHello, World!");
conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\n\r\nHello, World!");
logInfo("flush");
conn.flush(); conn.flush();
logInfo("wait for next request");
} }
logInfo("out");
} catch (Exception e) { } catch (Exception e) {
scope (failure) assert(false); scope (failure) assert(false);
logError("Error processing request: %s", e.msg); logError("Error processing request: %s", e.msg);
@ -33,8 +29,9 @@ void main()
} }
auto listener = listenTCP(8080, &staticAnswer, "127.0.0.1"); auto listener = listenTCP(8080, &staticAnswer, "127.0.0.1");
logInfo("Listening to HTTP requests on http://127.0.0.1:8080/");
runEventLoop(); runApplication();
} }
struct CountingRange { struct CountingRange {
@ -44,21 +41,16 @@ struct CountingRange {
void put(in ubyte[] arr) { count += arr.length; } void put(in ubyte[] arr) { count += arr.length; }
} }
import std.range.primitives : isOutputRange;
void readLine(R, InputStream)(InputStream stream, ref R dst, size_t max_bytes = size_t.max) void readLine(R, InputStream)(InputStream stream, ref R dst, size_t max_bytes = size_t.max)
if (isOutputRange!(R, ubyte)) if (isInputStream!InputStream && isOutputRange!(R, ubyte))
{ {
import std.algorithm.comparison : min, max; import std.algorithm.comparison : min, max;
import std.algorithm.searching : countUntil; import std.algorithm.searching : countUntil;
enum end_marker = "\r\n"; enum end_marker = "\r\n";
enum nmarker = end_marker.length;
assert(end_marker.length >= 1 && end_marker.length <= 2);
size_t nmatched = 0; size_t nmatched = 0;
size_t nmarker = end_marker.length;
while (true) { while (true) {
enforce(!stream.empty, "Reached EOF while searching for end marker."); enforce(!stream.empty, "Reached EOF while searching for end marker.");
@ -83,6 +75,8 @@ void readLine(R, InputStream)(InputStream stream, ref R dst, size_t max_bytes =
if (nmatched == nmarker) return; if (nmatched == nmarker) return;
} }
} else { } else {
assert(nmatched == 0);
auto idx = pm.countUntil(end_marker[0]); auto idx = pm.countUntil(end_marker[0]);
if (idx < 0) { if (idx < 0) {
dst.put(pm); dst.put(pm);
@ -90,29 +84,15 @@ void readLine(R, InputStream)(InputStream stream, ref R dst, size_t max_bytes =
stream.skip(pm.length); stream.skip(pm.length);
} else { } else {
dst.put(pm[0 .. idx]); dst.put(pm[0 .. idx]);
stream.skip(idx+1); if (idx+1 < pm.length && pm[idx+1] == end_marker[1]) {
if (nmarker == 2) { assert(nmarker == 2);
ubyte[1] next; stream.skip(idx+2);
stream.read(next);
if (next[0] == end_marker[1])
return; return;
dst.put(end_marker[0]); } else {
dst.put(next[0]); nmatched++;
} else return; stream.skip(idx+1);
} }
} }
} }
} }
static if (!is(typeof(TCPConnection.init.skip(0))))
{
private void skip(ref TCPConnection str, ulong count)
{
ubyte[156] buf = void;
while (count > 0) {
auto n = min(buf.length, count);
str.read(buf[0 .. n]);
count -= n;
}
}
} }