Back to index

avfs  1.0.1
Classes | Functions
http_basic.h File Reference
#include <sys/types.h>
#include <stdio.h>
#include "http_request.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  http_content_type
struct  http_server_capabilities
struct  http_content_range

Functions

BEGIN_NEON_DECLS int http_put (http_session *sess, const char *uri, FILE *f)
int http_put_if_unmodified (http_session *sess, const char *uri, FILE *stream, time_t modtime)
int http_get (http_session *sess, const char *uri, FILE *f)
int http_read_file (http_session *sess, const char *uri, http_block_reader reader, void *userdata)
int http_getmodtime (http_session *sess, const char *uri, time_t *modtime)
void http_content_type_handler (void *userdata, const char *value)
int http_options (http_session *sess, const char *uri, http_server_capabilities *caps)
int http_get_range (http_session *sess, const char *uri, http_content_range *range, FILE *f)
int http_post (http_session *sess, const char *uri, FILE *f, const char *buffer)

Class Documentation

struct http_content_type

Definition at line 56 of file http_basic.h.

Class Members
const char * charset
const char * subtype
const char * type
char * value
struct http_server_capabilities

Definition at line 67 of file http_basic.h.

Class Members
unsigned int broken_expect100:1
unsigned int dav_class1
unsigned int dav_class2
unsigned int dav_executable
struct http_content_range

Definition at line 85 of file http_basic.h.

Class Members
off_t end
off_t start
off_t total

Function Documentation

void http_content_type_handler ( void *  userdata,
const char *  value 
)

Definition at line 417 of file http_basic.c.

{
    http_content_type *ct = userdata;
    char *sep, *parms;

    ct->value = ne_strdup(value);
    
    sep = strchr(ct->value, '/');
    if (!sep) {
       HTTP_FREE(ct->value);
       return;
    }

    *++sep = '\0';
    ct->type = ct->value;
    ct->subtype = sep;
    
    parms = strchr(ct->value, ';');

    if (parms) {
       *parms = '\0';
       /* TODO: handle charset. */
    }
}
int http_get ( http_session *  sess,
const char *  uri,
FILE *  f 
)

Definition at line 311 of file http_basic.c.

{
    http_req *req = http_request_create(sess, "GET", uri);
    struct get_context ctx;
    int ret;

    ctx.total = -1;
    ctx.progress = 0;
    ctx.callback = get_to_fd;
    ctx.userdata = &ctx;
    ctx.file = f;
    ctx.error = 0;

    /* Read the value of the Content-Length header into ctx.total */
    http_add_response_header_handler(req, "Content-Length",
                                 http_handle_numeric_header,
                                 &ctx.total);
    
    http_add_response_body_reader(req, http_accept_2xx, get_callback, &ctx);

    ret = http_request_dispatch(req);
    
    if (ctx.error) {
       char buf[BUFSIZ];
       snprintf(buf, BUFSIZ, 
                _("Could not write to file: %s"), strerror(ctx.error));
       http_set_error(sess, buf);
       ret = HTTP_ERROR;
    }

    if (ret == HTTP_OK && http_get_status(req)->klass != 2) {
       ret = HTTP_ERROR;
    }

    http_request_destroy(req);

    return ret;
}

Here is the call graph for this function:

int http_get_range ( http_session *  sess,
const char *  uri,
http_content_range range,
FILE *  f 
)

Definition at line 252 of file http_basic.c.

{
    http_req *req = http_request_create(sess, "GET", uri);
    struct get_context ctx;
    int ret;

    if (range->end == -1) {
       ctx.total = -1;
    } 
    else {
       ctx.total = range->end - range->start;
    }

    ctx.progress = 0;
    ctx.callback = get_to_fd;
    ctx.userdata = &ctx;
    ctx.file = f;
    ctx.error = 0;
    ctx.range = range;

    http_add_response_header_handler(req, "Content-Length",
                                 clength_hdr_handler, &ctx);
    http_add_response_header_handler(req, "Content-Range",
                                 content_range_hdr_handler,
                                 &ctx);

    http_add_response_body_reader(req, accept_206, get_callback, &ctx);

    /* icky casts to long int, which should be at least as large as the
     * off_t's */
    if (range->end == -1) {
       http_print_request_header(req, "Range", "bytes=%ld-", 
                              (long int) range->start);
    }
    else {
       http_print_request_header(req, "Range", "bytes=%ld-%ld",
                              (long int) range->start, 
                              (long int)range->end);
    }
    http_add_request_header(req, "Accept-Ranges", "bytes");

    ret = http_request_dispatch(req);
    
    if (ret == HTTP_OK && http_get_status(req)->klass != 2) {
       ret = HTTP_ERROR;
    }
    else if (http_get_status(req)->code != 206) {
       http_set_error(sess, _("Server does not allow partial GETs."));
       ret = HTTP_ERROR;
    }
    
    http_request_destroy(req);

    return ret;
}

Here is the call graph for this function:

int http_getmodtime ( http_session *  sess,
const char *  uri,
time_t *  modtime 
)

Definition at line 52 of file http_basic.c.

{
    http_req *req = http_request_create(sess, "HEAD", uri);
    int ret;

    http_add_response_header_handler(req, "Last-Modified", get_lastmodified,
                                 modtime);

    *modtime = -1;

    ret = http_request_dispatch(req);

    if (ret == HTTP_OK && http_get_status(req)->klass != 2) {
       *modtime = -1;
       ret = HTTP_ERROR;
    }

    http_request_destroy(req);

    return ret;
}

Here is the call graph for this function:

int http_options ( http_session *  sess,
const char *  uri,
http_server_capabilities caps 
)

Definition at line 463 of file http_basic.c.

{
    http_req *req = http_request_create(sess, "OPTIONS", uri);
    
    int ret;

    http_add_response_header_handler(req, "Server", server_hdr_handler, caps);
    http_add_response_header_handler(req, "DAV", dav_hdr_handler, caps);

    ret = http_request_dispatch(req);
 
    if (ret == HTTP_OK && http_get_status(req)->klass != 2) {
       ret = HTTP_ERROR;
    }
    
    http_request_destroy(req);

    return ret;
}

Here is the call graph for this function:

int http_post ( http_session *  sess,
const char *  uri,
FILE *  f,
const char *  buffer 
)

Definition at line 352 of file http_basic.c.

{
    http_req *req = http_request_create(sess, "POST", uri);
    struct get_context ctx;
    int ret;

    ctx.total = -1;
    ctx.progress = 0;
    ctx.callback = get_to_fd;
    ctx.userdata = &ctx;
    ctx.file = f;
    ctx.error = 0;

    /* Read the value of the Content-Length header into ctx.total */
    http_add_response_header_handler(req, "Content-Length",
                                 http_handle_numeric_header, &ctx.total);

    http_add_response_body_reader(req, http_accept_2xx, get_callback, &ctx);

    http_set_request_body_buffer(req, buffer);

    ret = http_request_dispatch(req);
    
    if (ctx.error) {
       char buf[BUFSIZ];
       snprintf(buf, BUFSIZ, 
               _("Could not write to file: %s"), strerror(ctx.error));
       http_set_error(sess, buf);
       ret = HTTP_ERROR;
    }

    if (ret == HTTP_OK && http_get_status(req)->klass != 2) {
       ret = HTTP_ERROR;
    }

    http_request_destroy(req);

    return ret;
}

Here is the call graph for this function:

BEGIN_NEON_DECLS int http_put ( http_session *  sess,
const char *  uri,
FILE *  f 
)

Definition at line 75 of file http_basic.c.

{
    http_req *req = http_request_create(sess, "PUT", uri);
    int ret;
    
#ifdef USE_DAV_LOCKS
    dav_lock_using_resource(req, uri, 0);
    dav_lock_using_parent(req, uri);
#endif

    http_set_request_body_stream(req, stream);
       
    ret = http_request_dispatch(req);
    
    if (ret == HTTP_OK && http_get_status(req)->klass != 2)
       ret = HTTP_ERROR;

    http_request_destroy(req);

    return ret;
}
int http_put_if_unmodified ( http_session *  sess,
const char *  uri,
FILE *  stream,
time_t  modtime 
)

Definition at line 102 of file http_basic.c.

                                                 {
    http_req *req;
    char *date;
    int ret;
    
    if (http_version_pre_http11(sess)) {
       time_t modtime;
       /* Server is not minimally HTTP/1.1 compliant.  Do a HEAD to
        * check the remote mod time. Of course, this makes the
        * operation very non-atomic, but better than nothing. */
       ret = http_getmodtime(sess, uri, &modtime);
       if (ret != HTTP_OK) return ret;
       if (modtime != since)
           return HTTP_FAILED;
    }

    req = http_request_create(sess, "PUT", uri);

    date = rfc1123_date(since);
    /* Add in the conditionals */
    http_add_request_header(req, "If-Unmodified-Since", date);
    free(date);
    
#ifdef USE_DAV_LOCKS
    dav_lock_using_resource(req, uri, 0);
    /* FIXME: this will give 412 if the resource doesn't exist, since
     * PUT may modify the parent... does that matter?  */
#endif

    http_set_request_body_stream(req, stream);

    ret = http_request_dispatch(req);
    
    if (ret == HTTP_OK) {
       if (http_get_status(req)->code == 412) {
           ret = HTTP_FAILED;
       } else if (http_get_status(req)->klass != 2) {
           ret = HTTP_ERROR;
       }
    }

    http_request_destroy(req);

    return ret;
}
int http_read_file ( http_session *  sess,
const char *  uri,
http_block_reader  reader,
void *  userdata 
)

Definition at line 176 of file http_basic.c.

                                                           {
    struct get_context ctx;
    http_req *req = http_request_create(sess, "GET", uri);
    int ret;
    
    ctx.total = -1;
    ctx.progress = 0;
    ctx.callback = reader;
    ctx.userdata = userdata;

    /* Read the value of the Content-Length header into ctx.total */
    http_add_response_header_handler(req, "Content-Length",
                                 http_handle_numeric_header,
                                 &ctx.total);
    
    http_add_response_body_reader(req, http_accept_2xx, get_callback, &ctx);

    ret = http_request_dispatch(req);

    if (ret == HTTP_OK && http_get_status(req)->klass != 2)
       ret = HTTP_ERROR;

    http_request_destroy(req);

    return ret;
}

Here is the call graph for this function: