Skip to content

Commit

Permalink
adjust to review
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Apr 19, 2019
1 parent 1f8635b commit e0317a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
6 changes: 3 additions & 3 deletions http/vibe/http/client.d
Expand Up @@ -246,9 +246,9 @@ auto connectHTTP(string host, ushort port = 0, bool use_tls = false, const(HTTPC
static ~this()
{
foreach (ci; s_connections) {
ci[1].removeUnused((conn) {
conn.disconnect();
});
// ci[1].removeUnused((conn) {
// conn.disconnect();
// });
}
}

Expand Down
63 changes: 44 additions & 19 deletions http/vibe/http/fileserver.d
Expand Up @@ -16,12 +16,13 @@ import vibe.inet.mimetypes;
import vibe.inet.url;
import vibe.internal.interfaceproxy;

import std.algorithm;
import std.conv;
import std.datetime;
import std.digest.md;
import std.exception;
import std.range : popFront, empty, drop;
import std.string;
import std.algorithm;

@safe:

Expand Down Expand Up @@ -502,7 +503,7 @@ bool handleCache(scope HTTPServerRequest req, scope HTTPServerResponse res, ETag
res.headers["Cache-Control"] = cache_control;
}
} else if (max_age > Duration.zero) {
res.headers["Cache-Control"] = "max-age=" ~ to!string(max_age.total!"seconds");
res.headers["Cache-Control"] = text("max-age=", max_age.total!"seconds");
}

// https://tools.ietf.org/html/rfc7232#section-3.1
Expand Down Expand Up @@ -532,8 +533,8 @@ bool handleCache(scope HTTPServerRequest req, scope HTTPServerResponse res, ETag
// https://tools.ietf.org/html/rfc7232#section-3.3
string ifModifiedSince = req.headers.get("If-Modified-Since");
if (ifModifiedSince.length) {
const check = lastModifiedString == ifModifiedSince
|| last_modified <= parseRFC822DateTimeString(ifModifiedSince);
const check = lastModifiedString == ifModifiedSince ||
last_modified <= parseRFC822DateTimeString(ifModifiedSince);
if (check) {
res.statusCode = HTTPStatus.notModified;
res.writeVoidBody();
Expand Down Expand Up @@ -572,9 +573,7 @@ struct ETag
enforce!ConvException(s.endsWith('"'));

if (s.startsWith(`W/"`)) {
ETag ret;
ret.weak = true;
ret.tag = s[3 .. $ - 1];
ETag ret = { weak: true, tag: s[3 .. $ - 1] };
return ret;
} else if (s.startsWith('"')) {
ETag ret;
Expand Down Expand Up @@ -602,30 +601,56 @@ bool cacheMatch(string match, ETag etag, bool allow_weak)
return true;
}

if (etag.weak && !allow_weak) {
if ((etag.weak && !allow_weak) || !match.length) {
return false;
}

size_t i = match.indexOf('"');
while (i != -1) {
size_t end = match.indexOf('"', i + 1);
if (end == -1) {
end = match.length;
}
auto allBytes = match.representation;

for (auto range = allBytes; !range.empty; range.popFront)
{
auto start = range = range.find('"').drop(1);
range = range.find('"');
if (range.empty)
break;

const check = match[i + 1 .. end];
const checkWeak = match[0 .. i].endsWith("W/");
const check = start[0 .. &range[0] - &start[0]];
const checkWeak = match[0 .. &start[0] - &allBytes[0]].endsWith(`W/"`);

if (allow_weak || !checkWeak) {
if (match[i + 1 .. end] == etag.tag) {
if (check == etag.tag) {
return true;
}
}

if (!match[end + 1 .. $].stripLeft.startsWith(","))
if (!(cast(string) range).stripLeft.startsWith(","))
break;
i = match.indexOf('"', end + 1);
}

return false;
}

unittest
{
// from RFC 7232 Section 2.3.2
// +--------+--------+-------------------+-----------------+
// | ETag 1 | ETag 2 | Strong Comparison | Weak Comparison |
// +--------+--------+-------------------+-----------------+
// | W/"1" | W/"1" | no match | match |
// | W/"1" | W/"2" | no match | no match |
// | W/"1" | "1" | no match | match |
// | "1" | "1" | match | match |
// +--------+--------+-------------------+-----------------+

assert(!cacheMatch(`W/"1"`, ETag(true, "1"), false));
assert( cacheMatch(`W/"1"`, ETag(true, "1"), true));

assert(!cacheMatch(`W/"1"`, ETag(true, "2"), false));
assert(!cacheMatch(`W/"1"`, ETag(true, "2"), true));

assert(!cacheMatch(`W/"1"`, ETag(false, "1"), false));
assert( cacheMatch(`W/"1"`, ETag(false, "1"), true));

assert(cacheMatch(`"1"`, ETag(false, "1"), false));
assert(cacheMatch(`"1"`, ETag(false, "1"), true));
}

0 comments on commit e0317a7

Please sign in to comment.