/*static*/ nsresult
nsHttpHeaderArray::ParseHeaderLine(const nsACString& line,
                                   nsHttpAtom *hdr,
                                   nsACString *headerName,
                                   nsACString *val)
{
    //
    // BNF from section 4.2 of RFC 2616:
    //
    //   message-header = field-name ":" [ field-value ]
    //   field-name     = token
    //   field-value    = *( field-content | LWS )
    //   field-content  = <the OCTETs making up the field-value
    //                     and consisting of either *TEXT or combinations
    //                     of token, separators, and quoted-string>
    //

    // We skip over mal-formed headers in the hope that we'll still be able to
    // do something useful with the response.
    int32_t split = line.FindChar(':');

    if (split == kNotFound) {
        LOG(("malformed header [%s]: no colon\n",
            PromiseFlatCString(line).get()));
        return NS_ERROR_FAILURE;
    }

    const nsACString& sub = Substring(line, 0, split);
    const nsACString& sub2 = Substring(
        line, split + 1, line.Length() - split - 1);

    // make sure we have a valid token for the field-name
    if (!nsHttp::IsValidToken(sub)) {
        LOG(("malformed header [%s]: field-name not a token\n",
            PromiseFlatCString(line).get()));
        return NS_ERROR_FAILURE;
    }

    nsHttpAtom atom = nsHttp::ResolveAtom(sub);
    if (!atom) {
        LOG(("failed to resolve atom [%s]\n", PromiseFlatCString(line).get()));
        return NS_ERROR_FAILURE;
    }

    // skip over whitespace
    char *p = net_FindCharNotInSet(
        sub2.BeginReading(), sub2.EndReading(), HTTP_LWS);

    // trim trailing whitespace - bug 86608
    char *p2 = net_RFindCharNotInSet(p, sub2.EndReading(), HTTP_LWS);

    // assign return values
    if (hdr) *hdr = atom;
    if (val) val->Assign(p, p2 - p + 1);
    if (headerName) headerName->Assign(sub);

    return NS_OK;
}
示例#2
0
nsresult
nsHttpHeaderArray::ParseHeaderLine(const char *line,
                                   nsHttpAtom *hdr,
                                   char **val)
{
    //
    // BNF from section 4.2 of RFC 2616:
    //
    //   message-header = field-name ":" [ field-value ]
    //   field-name     = token
    //   field-value    = *( field-content | LWS )
    //   field-content  = <the OCTETs making up the field-value
    //                     and consisting of either *TEXT or combinations
    //                     of token, separators, and quoted-string>
    //

    // We skip over mal-formed headers in the hope that we'll still be able to
    // do something useful with the response.

    char *p = (char *) strchr(line, ':');
    if (!p) {
        LOG(("malformed header [%s]: no colon\n", line));
        return NS_OK;
    }

    // make sure we have a valid token for the field-name
    if (!nsHttp::IsValidToken(line, p)) {
        LOG(("malformed header [%s]: field-name not a token\n", line));
        return NS_OK;
    }

    *p = 0; // null terminate field-name

    nsHttpAtom atom = nsHttp::ResolveAtom(line);
    if (!atom) {
        LOG(("failed to resolve atom [%s]\n", line));
        return NS_OK;
    }

    // skip over whitespace
    p = net_FindCharNotInSet(++p, HTTP_LWS);

    // trim trailing whitespace - bug 86608
    char *p2 = net_RFindCharNotInSet(p, HTTP_LWS);

    *++p2 = 0; // null terminate header value; if all chars starting at |p|
               // consisted of LWS, then p2 would have pointed at |p-1|, so
               // the prefix increment is always valid.

    // assign return values
    if (hdr) *hdr = atom;
    if (val) *val = p;

    // assign response header
    return SetHeaderFromNet(atom, nsDependentCString(p, p2 - p));
}