Skip to content

Commit

Permalink
Send vidyano client version cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleeckx committed Mar 2, 2017
1 parent 67755f6 commit 9648a00
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions dist/Vidyano.Web2/Web2Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public HttpResponseMessage Get(string id = null)
{
case ".html":
var html = Vulcanizer.Generate(id, File.ReadAllText(filePath), true, false);
return new HttpResponseMessage { Content = new StringContent(html, Encoding.UTF8, mediaTypes[extension]) };
return new HttpResponseMessage { Content = new StringContent(html, Encoding.UTF8, mediaTypes[extension]) }.AddVersion(id);

case ".css":
case ".js":
Expand All @@ -79,7 +79,7 @@ public HttpResponseMessage Get(string id = null)
var ifNoneMatch = Request.Headers.IfNoneMatch.FirstOrDefault();
Tuple<string, string> cacheInfo;
if (cache.TryGetValue(id, out cacheInfo) && ifNoneMatch?.Tag == cacheInfo.Item1)
return new HttpResponseMessage(HttpStatusCode.NotModified);
return new HttpResponseMessage(HttpStatusCode.NotModified).AddVersion(id);

string content;
lock (syncRoot)
Expand Down Expand Up @@ -127,7 +127,7 @@ public HttpResponseMessage Get(string id = null)
httpContent = new CompressedContent(httpContent, encoding);
}

var response = new HttpResponseMessage { Content = httpContent };
var response = new HttpResponseMessage { Content = httpContent }.AddVersion(id);
response.Headers.ETag = new EntityTagHeaderValue(cacheInfo.Item1);

return response;
Expand Down Expand Up @@ -210,4 +210,26 @@ private static string GetSHA256(string content)
return sb.ToString();
}
}

static class HttpResponseMessageEx
{
private static readonly string version = typeof(Web2Controller).Assembly.GetName().Version.ToString(3);

public static HttpResponseMessage AddVersion(this HttpResponseMessage message, string id)
{
if (string.Equals(id, "vidyano.html"))
{
var cookie = new CookieHeaderValue("__vi", version)
{
HttpOnly = true,
Path = "/",
Expires = DateTimeOffset.Now.AddYears(1)
};

message.Headers.TryAddWithoutValidation("Set-Cookie", cookie.ToString());
}

return message;
}
}
}

0 comments on commit 9648a00

Please sign in to comment.