Ejemplo n.º 1
0
/* Dispatch a GET request REQ, writing the response body to FD fd.  If
 * RANGE is non-NULL, then it is the value of the Range request
 * header, e.g. "bytes=1-5".  Returns an NE_* error code. */
static int dispatch_to_fd(ne_request *req, int fd, const char *range)
{
    ne_session *const sess = ne_get_session(req);
    const ne_status *const st = ne_get_status(req);
    int ret;

    do {
        const char *value;
        
        ret = ne_begin_request(req);
        if (ret != NE_OK) break;

        value = ne_get_response_header(req, "Content-Range");

        /* For a 206 response, check that a Content-Range header is
         * given which matches the Range request header. */
        if (range && st->code == 206 
            && (value == NULL || strncmp(value, "bytes ", 6) != 0
                || strcmp(range + 6, value + 6))) {
            ne_set_error(sess, _("Response did not include requested range"));
            return NE_ERROR;
        }

        if ((range && st->code == 206) || (!range && st->klass == 2)) {
            ret = ne_read_response_to_fd(req, fd);
        } else {
            ret = ne_discard_response(req);
        }

        if (ret == NE_OK) ret = ne_end_request(req);
    } while (ret == NE_RETRY);

    return ret;
}
Ejemplo n.º 2
0
int ne_xml_dispatch_request(ne_request *req, ne_xml_parser *parser)
{
    int ret;

    do {
        int parseit = 0;

        ret = ne_begin_request(req);
        if (ret) break;
        
        if (ne_get_status(req)->klass == 2) {
            ne_content_type ctype;
            
            if (ne_get_content_type(req, &ctype) == 0) {
                parseit = media_type_is_xml(&ctype);
                ne_free(ctype.value);
            }
        }

        if (parseit)
            ret = ne_xml_parse_response(req, parser);
        else
            ret = ne_discard_response(req);
        
        if (ret == NE_OK)
            ret = ne_end_request(req);
    } while (ret == NE_RETRY);

    return ret;
}
Ejemplo n.º 3
0
/* Dispatch a GET request REQ, writing the response body to FD fd.  If
 * RANGE is non-NULL, then it is the value of the Range request
 * header, e.g. "bytes=1-5".  Returns an NE_* error code. */
static int dispatch_to_fd(ne_request *req, int fd, const char *range)
{
    ne_session *const sess = ne_get_session(req);
    const ne_status *const st = ne_get_status(req);
    int ret;

    do {
        const char *value;
        
        ret = ne_begin_request(req);
        if (ret != NE_OK) break;

        value = ne_get_response_header(req, "Content-Range");

        /* For a 206 response, check that a Content-Range header is
         * given which matches the Range request header. */
        if (range && st->code == 206) {
            int err = 0;
            if (value == NULL || strncmp(value, "bytes ", 6) != 0) {
                err++;
            } else {
                /* If the response gives a range begin-end/total, limit
                 * the comparison to the range itself. */
                int len = strlen(value);
                char *cp = strchr(value, '/');
                if (cp != NULL)
                    len = (int)(cp - value);
                len -= 6;
                if (strncmp(range + 6, value + 6, len))
                    err++;
            }
            if (err) {
                ne_set_error(sess, _("Response did not include requested range"));
                return NE_ERROR;
            }
        }

        if ((range && st->code == 206) || (!range && st->klass == 2)) {
            ret = ne_read_response_to_fd(req, fd);
        } else {
            ret = ne_discard_response(req);
        }

        if (ret == NE_OK) ret = ne_end_request(req);
    } while (ret == NE_RETRY);

    return ret;
}
Ejemplo n.º 4
0
int ne_xml_dispatch_request(ne_request *req, ne_xml_parser *parser)
{
    int ret;

    do {
        ret = ne_begin_request(req);
        if (ret) break;

        if (ne_get_status(req)->klass == 2)
            ret = ne_xml_parse_response(req, parser);
        else
            ret = ne_discard_response(req);
        
        if (ret == NE_OK)
            ret = ne_end_request(req);
    } while (ret == NE_RETRY);

    return ret;
}
Ejemplo n.º 5
0
static int dispatch_to_buffer(ne_session *sess, ne_request *req, char *buf, const char *range, ssize_t *bytes_read)
{
    const ne_status *const st = ne_get_status(req);
    int ret;
    size_t rlen;

    /* length of bytespec after "bytes=" */
    rlen = range ? strlen(range + 6) : 0;

    do {
        const char *value;

        ret = ne_begin_request(req);
        if (ret != NE_OK) break;

        value = ne_get_response_header(req, "Content-Range");

        /* For a 206 response, check that a Content-Range header is
         * given which matches the Range request header. */
        if (range && st->code == 206
            && (value == NULL || strncmp(value, "bytes ", 6) != 0
                || strncmp(range + 6, value + 6, rlen)
                || (range[5 + rlen] != '-' && value[6 + rlen] != '/'))) {
            ne_set_error(sess, "Response did not include requested range");
            return NE_ERROR;
        }

        if ((range && st->code == 206) || (!range && st->klass == 2)) {
            ret = ne_read_response_to_buf(req, buf, bytes_read);
        } else {
            ret = ne_discard_response(req);
        }

        if (ret == NE_OK) ret = ne_end_request(req);
    } while (ret == NE_RETRY);

    return ret;
}