本文整理汇总了C#中IHttpContext.MatchEtag方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpContext.MatchEtag方法的具体用法?C# IHttpContext.MatchEtag怎么用?C# IHttpContext.MatchEtag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpContext
的用法示例。
在下文中一共展示了IHttpContext.MatchEtag方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Respond
public override void Respond(IHttpContext context)
{
var parameters = GetParametersFromPath(context.GetRequestUrl(), context.Request.QueryString);
var index = Database.IndexStorage.GetIndexInstance(parameters.IndexName);
if (index == null)
{
context.SetStatusToNotFound();
context.WriteJson(new { Error = "The index " + parameters.IndexName + " cannot be found" });
return;
}
var indexEtag = Database.GetIndexEtag(parameters.IndexName, null);
if (context.MatchEtag(indexEtag))
{
context.SetStatusToNotModified();
return;
}
var result = Database.ExecuteMoreLikeThisQuery(parameters, GetRequestTransaction(context), context.GetPageSize(Database.Configuration.MaxPageSize), context.Request.QueryString.GetValues("include"));
if (context.MatchEtag(result.Etag))
{
context.SetStatusToNotModified();
return;
}
context.Response.AddHeader("ETag", result.Etag.ToString());
context.WriteJson(result.Result);
}
示例2: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var filename = match.Groups[1].Value;
var etag = context.GetEtag();
switch (context.Request.HttpMethod)
{
case "GET":
Database.TransactionalStorage.Batch(_=> // have to keep the session open for reading of the attachment stream
{
var attachmentAndHeaders = Database.GetStatic(filename);
if (attachmentAndHeaders == null)
{
context.SetStatusToNotFound();
return;
}
if (context.MatchEtag(attachmentAndHeaders.Etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteHeaders(attachmentAndHeaders.Metadata, attachmentAndHeaders.Etag);
using (var stream = attachmentAndHeaders.Data())
{
stream.CopyTo(context.Response.OutputStream);
}
});
break;
case "HEAD":
Database.TransactionalStorage.Batch(_ => // have to keep the session open for reading of the attachment stream
{
var attachmentAndHeaders = Database.GetStatic(filename);
if (attachmentAndHeaders == null)
{
context.SetStatusToNotFound();
return;
}
if (context.MatchEtag(attachmentAndHeaders.Etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteHeaders(attachmentAndHeaders.Metadata, attachmentAndHeaders.Etag);
context.Response.ContentLength64 = attachmentAndHeaders.Size;
});
break;
case "PUT":
var newEtag = Database.PutStatic(filename, context.GetEtag(), context.Request.InputStream,
context.Request.Headers.FilterHeaders(isServerDocument:false));
context.WriteETag(newEtag);
context.SetStatusToCreated("/static/" + filename);
break;
case "DELETE":
Database.DeleteStatic(filename, etag);
context.SetStatusToDeleted();
break;
}
}
示例3: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[1].Value;
var facetSetupDoc = context.GetFacetSetupDocFromHttpContext();
var indexQuery = context.GetIndexQueryFromHttpContext(Database.Configuration.MaxPageSize);
var jsonDocument = Database.Get(facetSetupDoc, null);
if(jsonDocument == null)
{
context.SetStatusToNotFound();
context.Write("Could not find facet document: " + facetSetupDoc);
return;
}
var etag = GetFacetsEtag(jsonDocument, index);
if(context.MatchEtag(etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteETag(etag);
context.WriteJson(Database.ExecuteGetTermsQuery(index, indexQuery, facetSetupDoc));
}
示例4: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[1].Value;
var indexQuery = context.GetIndexQueryFromHttpContext(Database.Configuration.MaxPageSize);
var facetStart = context.GetFacetStartFromHttpContext();
var facetPageSize = context.GetFacetPageSizeFromHttpContext();
List<Facet> facets;
Etag etag;
if (TryGetFacets(context, index, out etag, out facets) == false)
return;
if (context.MatchEtag(etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteETag(etag);
context.WriteJson(Database.ExecuteGetTermsQuery(index, indexQuery, facets, facetStart, facetPageSize));
}
示例5: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var filename = match.Groups[1].Value;
var etag = context.GetEtag();
switch (context.Request.HttpMethod)
{
case "GET":
var attachmentAndHeaders = Database.GetStatic(filename);
if (attachmentAndHeaders == null)
{
context.SetStatusToNotFound();
return;
}
if (context.MatchEtag(attachmentAndHeaders.Etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteData(attachmentAndHeaders.Data, attachmentAndHeaders.Metadata,
attachmentAndHeaders.Etag);
break;
case "PUT":
Database.PutStatic(filename, context.GetEtag(), context.Request.InputStream.ReadData(),
context.Request.Headers.FilterHeaders(isServerDocument:false));
context.SetStatusToCreated("/static/" + filename);
break;
case "DELETE":
Database.DeleteStatic(filename, etag);
context.SetStatusToDeleted();
break;
}
}
示例6: Get
private void Get(IHttpContext context, string docId)
{
context.Response.AddHeader("Content-Type", "application/json; charset=utf-8");
if (string.IsNullOrEmpty(context.Request.Headers["If-None-Match"]))
{
GetDocumentDirectly(context, docId);
return;
}
Database.TransactionalStorage.Batch(
_ => // we are running this here to ensure transactional safety for the two operations
{
var transactionInformation = GetRequestTransaction(context);
var documentMetadata = Database.GetDocumentMetadata(docId, transactionInformation);
if (documentMetadata == null)
{
context.SetStatusToNotFound();
return;
}
if (context.MatchEtag(documentMetadata.Etag))
{
context.SetStatusToNotModified();
return;
}
if (documentMetadata.NonAuthoritiveInformation)
{
context.SetStatusToNonAuthoritiveInformation();
}
GetDocumentDirectly(context, docId);
});
}
示例7: Respond
public override void Respond(IHttpContext context)
{
RavenJArray itemsToLoad;
if(context.Request.HttpMethod == "POST")
itemsToLoad = context.ReadJsonArray();
else
itemsToLoad = new RavenJArray(context.Request.QueryString.GetValues("id"));
var result = new MultiLoadResult();
var loadedIds = new HashSet<string>();
var includes = context.Request.QueryString.GetValues("include") ?? new string[0];
var transactionInformation = GetRequestTransaction(context);
var includedEtags = new List<byte>();
Database.TransactionalStorage.Batch(actions =>
{
foreach (RavenJToken item in itemsToLoad)
{
var value = item.Value<string>();
if(loadedIds.Add(value)==false)
continue;
var documentByKey = Database.Get(value, transactionInformation);
if (documentByKey == null)
continue;
result.Results.Add(documentByKey.ToJson());
if (documentByKey.Etag != null)
{
includedEtags.AddRange(documentByKey.Etag.Value.ToByteArray());
}
includedEtags.Add((documentByKey.NonAuthoritativeInformation ?? false) ? (byte)0 : (byte)1);
}
var addIncludesCommand = new AddIncludesCommand(Database, transactionInformation, (etag, includedDoc) =>
{
includedEtags.AddRange(etag.ToByteArray());
result.Includes.Add(includedDoc);
}, includes, loadedIds);
foreach (var item in result.Results.Where(item => item != null))
{
addIncludesCommand.Execute(item);
}
});
Guid computedEtag;
using (var md5 = MD5.Create())
{
var computeHash = md5.ComputeHash(includedEtags.ToArray());
computedEtag = new Guid(computeHash);
}
if (context.MatchEtag(computedEtag))
{
context.SetStatusToNotModified();
return;
}
context.WriteETag(computedEtag);
context.WriteJson(result);
}
示例8: Respond
public override void Respond(IHttpContext context)
{
if (EnsureSystemDatabase(context) == false)
return;
// This responder is NOT secured, and anyone can access it.
// Because of that, we need to provide explicit security here.
// Anonymous Access - All / Get / Admin
// Show all dbs
// Anonymous Access - None
// Show only the db that you have access to (read / read-write / admin)
// If admin, show all dbs
List<string> approvedDatabases = null;
var databases = Database.GetDocumentsWithIdStartingWith("Raven/Databases/", null, null, context.GetStart(),
context.GetPageSize(Database.Configuration.MaxPageSize),
CancellationToken.None);
var data = databases
.Select(x => x.Value<RavenJObject>("@metadata").Value<string>("@id").Replace("Raven/Databases/", string.Empty))
.ToArray();
if (server.SystemConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.None)
{
var user = server.RequestAuthorizer.GetUser(context);
if(user == null)
{
return;
}
if (user.IsAdministrator(server.SystemConfiguration.AnonymousUserAccessMode) == false)
{
approvedDatabases = server.RequestAuthorizer.GetApprovedDatabases(user, context, data);
}
}
Etag lastDocEtag = Etag.Empty;
Database.TransactionalStorage.Batch(accessor =>
{
lastDocEtag = accessor.Staleness.GetMostRecentDocumentEtag();
});
if (context.MatchEtag(lastDocEtag))
{
context.SetStatusToNotModified();
}
else
{
context.WriteHeaders(new RavenJObject(), lastDocEtag);
if(approvedDatabases != null)
{
data = data.Where(s => approvedDatabases.Contains(s)).ToArray();
}
context.WriteJson(data);
}
}
示例9: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var userId = match.Groups[1].Value;
var docIds = context.Request.QueryString.GetValues("id");
var operation = context.Request.QueryString["operation"];
var transactionInformation = GetRequestTransaction(context);
if (docIds == null || string.IsNullOrEmpty(operation) || string.IsNullOrEmpty(userId))
{
context.SetStatusToBadRequest();
return;
}
// we don't want security to take hold when we are trying to ask about security
using (Database.DisableAllTriggersForCurrentThread())
{
var documents = docIds.Select(docId=>Database.GetDocumentMetadata(docId, transactionInformation)).ToArray();
var etag = CalculateEtag(documents, userId);
if (context.MatchEtag(etag))
{
context.SetStatusToNotModified();
return;
}
context.Response.AddHeader("ETag", etag.ToString());
context.WriteJson(GenerateAuthorizationResponse(documents, docIds, operation, userId));
}
}
示例10: Respond
/// <summary>
/// Responds the specified context.
/// </summary>
/// <param name="context">The context.</param>
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[1].Value;
var indexEtag = Database.GetIndexEtag(index, null);
if (context.MatchEtag(indexEtag))
{
context.SetStatusToNotModified();
return;
}
var term = context.Request.QueryString["term"];
var field = context.Request.QueryString["field"];
StringDistanceTypes distanceTypes;
int numOfSuggestions;
float accuracy;
if (Enum.TryParse(context.Request.QueryString["distance"], true, out distanceTypes) == false)
distanceTypes = StringDistanceTypes.Default;
if (distanceTypes == StringDistanceTypes.None)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Suggestion is disabled since you specified the Distance value as 'StringDistanceTypes.None'."
});
return;
}
if (int.TryParse(context.Request.QueryString["max"], out numOfSuggestions) == false)
numOfSuggestions = 10;
if (float.TryParse(context.Request.QueryString["accuracy"], out accuracy) == false)
accuracy = 0.5f;
bool popularity;
if (bool.TryParse(context.Request.QueryString["popularity"], out popularity) == false)
popularity = false;
var query = new SuggestionQuery
{
Distance = distanceTypes,
Field = field,
MaxSuggestions = numOfSuggestions,
Term = term,
Accuracy = accuracy,
Popularity = popularity
};
var suggestionQueryResult = Database.ExecuteSuggestionQuery(index, query);
context.WriteETag(Database.GetIndexEtag(index, null));
context.WriteJson(suggestionQueryResult);
}
示例11: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var docId = match.Groups[1].Value;
switch (context.Request.HttpMethod)
{
case "GET":
context.Response.Headers["Content-Type"] = "application/json; charset=utf-8";
var doc = Database.Get(docId,GetRequestTransaction(context));
if (doc == null)
{
context.SetStatusToNotFound();
return;
}
if(doc.NonAuthoritiveInformation)
{
context.SetStatusToNonAuthoritiveInformation();
}
if (context.MatchEtag(doc.Etag))
{
context.SetStatusToNotModified();
return;
}
doc.Metadata["Last-Modified"] = doc.LastModified.ToString("r");
context.WriteData(doc.DataAsJson, doc.Metadata, doc.Etag);
break;
case "DELETE":
Database.Delete(docId, context.GetEtag(), GetRequestTransaction(context));
context.SetStatusToDeleted();
break;
case "PUT":
Put(context, docId);
break;
case "PATCH":
var patchRequestJson = context.ReadJsonArray();
var patchRequests = patchRequestJson.Cast<JObject>().Select(PatchRequest.FromJson).ToArray();
var patchResult = Database.ApplyPatch(docId, context.GetEtag(), patchRequests, GetRequestTransaction(context));
switch (patchResult)
{
case PatchResult.DocumentDoesNotExists:
context.SetStatusToNotFound();
break;
case PatchResult.Patched:
context.Response.Headers["Location"] = Database.Configuration.GetFullUrl("/docs/" + docId);
context.WriteJson(new {Patched = true});
break;
default:
throw new ArgumentOutOfRangeException("Value " + patchResult + " is not understood");
}
break;
}
}
示例12: Respond
public override void Respond(IHttpContext context)
{
switch (context.Request.HttpMethod)
{
case "GET":
long documentsCount = 0;
Etag lastDocEtag = Etag.Empty;
Database.TransactionalStorage.Batch(accessor =>
{
lastDocEtag = accessor.Staleness.GetMostRecentDocumentEtag();
documentsCount = accessor.Documents.GetDocumentsCount();
});
lastDocEtag = lastDocEtag.HashWith(BitConverter.GetBytes(documentsCount));
if (context.MatchEtag(lastDocEtag))
{
context.SetStatusToNotModified();
}
else
{
context.WriteHeaders(new RavenJObject(), lastDocEtag);
using (var cts = new CancellationTokenSource())
{
cts.TimeoutAfter(Settings.DatbaseOperationTimeout);
var startsWith = context.Request.QueryString["startsWith"];
if (string.IsNullOrEmpty(startsWith))
context.WriteJson(Database.GetDocuments(context.GetStart(), context.GetPageSize(Database.Configuration.MaxPageSize), context.GetEtagFromQueryString(), cts.Token));
else
context.WriteJson(Database.GetDocumentsWithIdStartingWith(
startsWith,
context.Request.QueryString["matches"],
context.Request.QueryString["exclude"],
context.GetStart(),
context.GetPageSize(Database.Configuration.MaxPageSize),
cts.Token));
}
}
break;
case "POST":
var json = context.ReadJson();
var id = Database.Put(null, Etag.Empty, json,
context.Request.Headers.FilterHeaders(),
GetRequestTransaction(context));
context.SetStatusToCreated("/docs/" + Uri.EscapeUriString(id.Key));
context.WriteJson(id);
break;
}
}
示例13: Respond
public override void Respond(IHttpContext context)
{
switch (context.Request.HttpMethod)
{
case "GET":
long documentsCount = 0;
Guid lastDocEtag = Guid.Empty;
Database.TransactionalStorage.Batch(accessor =>
{
lastDocEtag = accessor.Staleness.GetMostRecentDocumentEtag();
documentsCount = accessor.Documents.GetDocumentsCount();
});
var array = lastDocEtag.ToByteArray().Concat(BitConverter.GetBytes(documentsCount)).ToArray();
using (var md5 = MD5.Create())
{
var hashed = md5.ComputeHash(array);
lastDocEtag = new Guid(hashed);
}
if (context.MatchEtag(lastDocEtag))
{
context.SetStatusToNotModified();
}
else
{
context.WriteHeaders(new RavenJObject(), lastDocEtag);
var startsWith = context.Request.QueryString["startsWith"];
if (string.IsNullOrEmpty(startsWith))
context.WriteJson(Database.GetDocuments(context.GetStart(), context.GetPageSize(Database.Configuration.MaxPageSize), context.GetEtagFromQueryString()));
else
context.WriteJson(Database.GetDocumentsWithIdStartingWith(
startsWith,
context.Request.QueryString["matches"],
context.GetStart(),
context.GetPageSize(Database.Configuration.MaxPageSize)));
}
break;
case "POST":
var json = context.ReadJson();
var id = Database.Put(null, Guid.Empty, json,
context.Request.Headers.FilterHeaders(),
GetRequestTransaction(context));
context.SetStatusToCreated("/docs/" + Uri.EscapeUriString(id.Key));
context.WriteJson(id);
break;
}
}
示例14: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[1].Value;
var indexEtag = Database.GetIndexEtag(index, null);
if (context.MatchEtag(indexEtag))
{
context.SetStatusToNotModified();
return;
}
var executeGetTermsQuery = Database.ExecuteGetTermsQuery(index,
context.Request.QueryString["field"],
context.Request.QueryString["fromValue"],
context.GetPageSize(Database.Configuration.MaxPageSize)
);
context.WriteETag(Database.GetIndexEtag(index, null));
context.WriteJson(executeGetTermsQuery);
}
示例15: Respond
/// <summary>
/// Responds the specified context.
/// </summary>
/// <param name="context">The context.</param>
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[1].Value;
var indexEtag = Database.GetIndexEtag(index, null);
if (context.MatchEtag(indexEtag))
{
context.SetStatusToNotModified();
return;
}
var term = context.Request.QueryString["term"];
var field = context.Request.QueryString["field"];
StringDistanceTypes distanceTypes;
int numOfSuggestions;
float accuracy;
if (Enum.TryParse(context.Request.QueryString["distance"], true, out distanceTypes) == false)
distanceTypes =StringDistanceTypes.Default;
if (int.TryParse(context.Request.QueryString["max"], out numOfSuggestions) == false)
numOfSuggestions = 10;
if(float.TryParse(context.Request.QueryString["accuracy"], out accuracy) == false)
accuracy = 0.5f;
var query = new SuggestionQuery
{
Distance = distanceTypes,
Field = field,
MaxSuggestions = numOfSuggestions,
Term = term,
Accuracy = accuracy
};
var suggestionQueryResult = Database.ExecuteSuggestionQuery(index, query);
context.Response.AddHeader("ETag", Database.GetIndexEtag(index, null).ToString());
context.WriteJson(suggestionQueryResult);
}