new exyus PutCondition checking

2007-09-17 @ 22:56#

i updated the exyus engine this evening to simplify the process of checking pre-conditions for HTTP PUT. now i have to utility methods (CheckPutUpdateConditions and CheckPutCreateConditions) that encapsulate the details of sorting out the various headers and coming up with a final determination on whether the method can continue. code is pretty simple. i relies on a struct that contains the headers passed by the client when making the PUT call:

public struct PutHeaders
{
    public string IfMatch;
    public string IfNoneMatch;
    public string IfUnmodifiedSince;
    public string IfModifiedSince;

    public PutHeaders(HttpContext ctx)
    {
        Utility util = new Utility();
        this.IfMatch = util.GetHttpHeader(Constants.hdr_if_match, ctx.Request.Headers);
        this.IfUnmodifiedSince = util.GetHttpHeader(Constants.hdr_if_unmodified_since, ctx.Request.Headers);
        this.IfNoneMatch = util.GetHttpHeader(Constants.hdr_if_none_match, ctx.Request.Headers);
        this.IfModifiedSince = util.GetHttpHeader(Constants.hdr_if_modified_since, ctx.Request.Headers);
    }
}

and here's the methods themselves

public void CheckPutCreateCondition(PutHeaders ph, bool allowcreate, string etag, ref string put_error, ref bool save_item)
{
    if (allowcreate == false)
    {
        put_error = "Cannot create using PUT.";
        save_item = false;
    }
    else if
        (
            (ph.IfNoneMatch == "*" && etag == string.Empty)  // anything
            ||
            (ph.IfNoneMatch == string.Empty)   // none passed
            ||
            (ph.IfNoneMatch != etag) // passed, but not equal
        )
        save_item = true;
    else
    {
        put_error = "Header mismatch (If-None-Match). Unable to create.";
        save_item = false;
    }
}

public void CheckPutUpdateCondition(PutHeaders ph, string etag, string last_mod, ref string put_error, ref bool save_item)
{
    // compare resource headers to client headers
    if (ph.IfMatch == string.Empty && ph.IfUnmodifiedSince == string.Empty)
    {
        // gotta gimme one of these
        put_error = "Missing If-Match and If-Unmodified-Since headers. Unable to update.";
        save_item = false;
    }
    else if (ph.IfMatch != string.Empty && etag != ph.IfMatch)
    {
        // etags must match
        put_error = "Header mismatch (If-Match). Unable to update.";
        save_item = false;
    }
    else if (ph.IfUnmodifiedSince != string.Empty && last_mod != ph.IfUnmodifiedSince)
    {
        // last mod date must match
        put_error = "Header mismatch (If-Unmodified-Since). Unable to update.";
        save_item = false;
    }
    else
    {
        save_item = true;
    }
}

code