Back to index

avfs  1.0.1
Classes | Functions
http_basic.c File Reference
#include "config.h"
#include <sys/types.h>
#include <errno.h>
#include "http_request.h"
#include "http_basic.h"
#include "dates.h"
#include "nsocket.h"
#include "neon_i18n.h"
#include "ne_alloc.h"

Go to the source code of this file.

Classes

struct  get_context

Functions

static void get_lastmodified (void *userdata, const char *value)
int http_getmodtime (http_session *sess, const char *uri, time_t *modtime)
int http_put (http_session *sess, const char *uri, FILE *stream)
int http_put_if_unmodified (http_session *sess, const char *uri, FILE *stream, time_t since)
static void get_callback (void *userdata, const char *block, size_t length)
int http_read_file (http_session *sess, const char *uri, http_block_reader reader, void *userdata)
static void get_to_fd (void *userdata, const char *block, size_t length)
static int accept_206 (void *ud, http_req *req, http_status *st)
static void clength_hdr_handler (void *ud, const char *value)
static void content_range_hdr_handler (void *ud, const char *value)
int http_get_range (http_session *sess, const char *uri, http_content_range *range, FILE *f)
int http_get (http_session *sess, const char *uri, FILE *f)
int http_post (http_session *sess, const char *uri, FILE *f, const char *buffer)
static void server_hdr_handler (void *userdata, const char *value)
void http_content_type_handler (void *userdata, const char *value)
static void dav_hdr_handler (void *userdata, const char *value)
int http_options (http_session *sess, const char *uri, http_server_capabilities *caps)

Class Documentation

struct get_context

Definition at line 149 of file http_basic.c.

Collaboration diagram for get_context:
Class Members
http_block_reader callback
int error
FILE * file
size_t progress
http_content_range * range
size_t total
void * userdata

Function Documentation

static int accept_206 ( void *  ud,
http_req *  req,
http_status st 
) [static]

Definition at line 222 of file http_basic.c.

{
    return (st->code == 206);
}

Here is the caller graph for this function:

static void clength_hdr_handler ( void *  ud,
const char *  value 
) [static]

Definition at line 227 of file http_basic.c.

{
    struct get_context *ctx = ud;
    off_t len = strtol(value, NULL, 10);
    
    if (ctx->range->end == -1) {
       ctx->range->end = ctx->range->start + len;
    }
    else if (len != (ctx->range->end - ctx->range->start)) {
       DEBUG(DEBUG_HTTP, "Expecting %ld bytes, got entity of length %ld\n", 
             (long int) (ctx->range->end - ctx->range->start), 
             (long int) len);
       ctx->error = 1;
    }
}

Here is the caller graph for this function:

static void content_range_hdr_handler ( void *  ud,
const char *  value 
) [static]

Definition at line 243 of file http_basic.c.

{
    struct get_context *ctx = ud;

    if (strncmp(value, "bytes ", 6) != 0) {
       ctx->error = 1;
    }
}

Here is the caller graph for this function:

static void dav_hdr_handler ( void *  userdata,
const char *  value 
) [static]

Definition at line 442 of file http_basic.c.

{
    char **classes, **class;
    http_server_capabilities *caps = userdata;
    
    classes = split_string(value, ',', HTTP_QUOTES, HTTP_WHITESPACE);
    for (class = classes; *class!=NULL; class++) {

       if (strcmp(*class, "1") == 0) {
           caps->dav_class1 = 1;
       } else if (strcmp(*class, "2") == 0) {
           caps->dav_class2 = 1;
       } else if (strcmp(*class, "<http://apache.org/dav/propset/fs/1>") == 0) {
           caps->dav_executable = 1;
       }
    }
    
    split_string_free(classes);

}

Here is the caller graph for this function:

static void get_callback ( void *  userdata,
const char *  block,
size_t  length 
) [static]

Definition at line 158 of file http_basic.c.

{
    struct get_context *ctx = userdata;

    DEBUG(DEBUG_HTTP, "Got progress: %d out of %d\n", 
          ctx->progress, ctx->total);

    (*ctx->callback)(ctx->userdata, block, length);

    /* Increase progress */
    ctx->progress += length;
    if (ctx->progress > ctx->total) {
       /* Reset the counter if we're uploading it again */
       ctx->progress -= ctx->total;
    }
    sock_call_progress(ctx->progress, ctx->total);
}

Here is the caller graph for this function:

static void get_lastmodified ( void *  userdata,
const char *  value 
) [static]

Definition at line 47 of file http_basic.c.

                                                                {
    time_t *modtime = userdata;
    *modtime = http_dateparse(value);
}

Here is the caller graph for this function:

static void get_to_fd ( void *  userdata,
const char *  block,
size_t  length 
) [static]

Definition at line 204 of file http_basic.c.

{
    struct get_context *ctx = userdata;
    FILE *f = ctx->file;
    size_t ret;
    if (!ctx->error) {
       while (length > 0) {
           ret = fwrite(block, 1, length, f);
           if (ret < 0) {
              ctx->error = errno;
              break;
           } else {
              length -= ret;
           }
       }
    }
}

Here is the caller graph for this function:

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:

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

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  since 
)

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:

static void server_hdr_handler ( void *  userdata,
const char *  value 
) [static]

Definition at line 392 of file http_basic.c.

{
    char **tokens = split_string(value, ' ', HTTP_QUOTES, NULL);
    http_server_capabilities *caps = userdata;
    int n;

    for (n = 0; tokens[n] != NULL; n++) {
       if (strncasecmp(tokens[n], "Apache/", 7) == 0 && 
           strlen(tokens[n]) > 11) { /* 12 == "Apache/1.3.0" */
           const char *ver = tokens[n] + 7;
           int count;
           char **vers;
           vers = split_string_c(ver, '.', NULL, NULL, &count);
           /* Apache/1.3.6 and before have broken Expect: 100 support */
           if (count > 1 && atoi(vers[0]) < 2 && 
              atoi(vers[1]) < 4 && atoi(vers[2]) < 7) {
              caps->broken_expect100 = 1;
           }
           split_string_free(vers);
       }
    }    
    
    split_string_free(tokens);
}

Here is the caller graph for this function: