Skip to content

Commit

Permalink
langtags download using etag instead of if-modified-since header sill…
Browse files Browse the repository at this point in the history
…sdev#987

+semver:minor
  • Loading branch information
elisunger committed Oct 14, 2022
1 parent ae28271 commit 97fe7fd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [SIL.Windows.Forms.SettingProtection] Deprecated ManageComponent method
- [SIL.Scripture] VerseRef.TrySetVerseUnicode: Improve handling of non-decimal numerals and surrogate pair numerals (#1000)
- [SIL.Windows.Forms.WritingSystems] Ignore deprecated region subtags in `ScriptRegionVariantView`(#763)
- [SIL.WritingSystems] Download of langtag.json handled with etag instead of IF-MODIFIED-SINCE Header(#987)

### Fixed

Expand Down
82 changes: 62 additions & 20 deletions SIL.WritingSystems/Sldr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ public static IReadOnlyKeyedCollection<string, SldrLanguageTagInfo> LanguageTags
}
}

public static void InitializeLanguageTags()
public static void InitializeLanguageTags(bool downloadLangTags = true)
{
LoadLanguageTagsIfNecessary();
if (downloadLangTags) LoadLanguageTags();
}

/// <summary>
Expand All @@ -409,49 +410,90 @@ private static void LoadLanguageTagsIfNecessary()
CreateSldrCacheDirectory();

cachedAllTagsPath = Path.Combine(SldrCachePath, "langtags.json");
string etagPath;
etagPath = Path.Combine(SldrCachePath, "langtags.json.etag");
var sinceTime = _embeddedAllTagsTime.ToUniversalTime();
if (File.Exists(cachedAllTagsPath))
{
var fileTime = File.GetLastWriteTimeUtc(cachedAllTagsPath);
if (sinceTime > fileTime)
{
// delete the old langtags.json file if a newer embedded one is available.
// this can happen if the application is upgraded to use a newer version of SIL.WritingSystems
// that has an updated embedded langtags.json file.
File.Delete(cachedAllTagsPath);
File.Delete(etagPath);
}
else
sinceTime = fileTime;
}
sinceTime += TimeSpan.FromSeconds(1);
try
{
if (_offlineMode)
throw new WebException("Test mode: SLDR offline so accessing cache", WebExceptionStatus.ConnectFailure);


}
_languageTags = new ReadOnlyKeyedCollection<string, SldrLanguageTagInfo>(ParseAllTagsJson(cachedAllTagsPath));
}

// get SLDR langtags.json from the SLDR api compressed
// it will throw WebException or have status HttpStatusCode.NotModified if file is unchanged or not get it
var langtagsUrl =
$"{SldrRepository}index.html?query=langtags&ext=json{StagingParameter}";
var webRequest = (HttpWebRequest) WebRequest.Create(Uri.EscapeUriString(langtagsUrl));
webRequest.UserAgent = UserAgent;
webRequest.IfModifiedSince = sinceTime;
webRequest.Timeout = 10000;
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using var webResponse = (HttpWebResponse) webRequest.GetResponse();
public static void LoadLanguageTags()
{
CreateSldrCacheDirectory();
string cachedAllTagsPath;
cachedAllTagsPath = Path.Combine(SldrCachePath, "langtags.json");
string etagPath;
etagPath = Path.Combine(SldrCachePath, "langtags.json.etag");
string etag;
string currentEtag;
try
{
if (_offlineMode)
throw new WebException("Test mode: SLDR offline so accessing cache", WebExceptionStatus.ConnectFailure);
// get SLDR langtags.json from the SLDR api compressed
// it will throw WebException or have status HttpStatusCode.NotModified if file is unchanged or not get it
var langtagsUrl =
$"{SldrRepository}index.html?query=langtags&ext=json{StagingParameter}";
var webRequest = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(langtagsUrl));
webRequest.UserAgent = UserAgent;
webRequest.Timeout = 10000;
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (File.Exists(etagPath))
{
etag = File.ReadAllText(etagPath);
currentEtag = webResponse.Headers.Get("Etag");
if (etag == "")
{
File.WriteAllText(etagPath, currentEtag);
}
else if (!etag.Equals(currentEtag))
{
File.WriteAllText(etagPath, etag);
webRequest.Headers.Set(etag, "If-None-Match");
if (webResponse.StatusCode != HttpStatusCode.NotModified)
{
using Stream output = File.OpenWrite(cachedAllTagsPath);
using var input = webResponse.GetResponseStream();
input.CopyTo(output);
}
}
}
else
{
currentEtag = webResponse.Headers.Get("Etag");
File.WriteAllText(etagPath, currentEtag);
if (webResponse.StatusCode != HttpStatusCode.NotModified)
{
using Stream output = File.OpenWrite(cachedAllTagsPath);
using var input = webResponse.GetResponseStream();
input.CopyTo(output);
}
}
catch (WebException)
{
}

}
catch (WebException)
{
}
_languageTags = new ReadOnlyKeyedCollection<string, SldrLanguageTagInfo>(ParseAllTagsJson(cachedAllTagsPath));
}


private static IKeyedCollection<string, SldrLanguageTagInfo> ParseAllTagsJson(string cachedAllTagsPath)
{
// read in the json file
Expand Down

0 comments on commit 97fe7fd

Please sign in to comment.