Exemplo n.º 1
0
static inline void *_mk_event_loop_create(int size)
{
    struct mk_event_ctx *ctx;

    /* Override caller 'size', we always use FD_SETSIZE */
    size = FD_SETSIZE;

    /* Main event context */
    ctx = mk_mem_alloc_z(sizeof(struct mk_event_ctx));
    if (!ctx) {
        return NULL;
    }

    FD_ZERO(&ctx->rfds);
    FD_ZERO(&ctx->wfds);

    /* Allocate space for events queue, re-use the struct mk_event */
    ctx->events = mk_mem_alloc_z(sizeof(struct mk_event *) * size);
    if (!ctx->events) {
        mk_mem_free(ctx);
        return NULL;
    }

    /* Fired events (upon select(2) return) */
    ctx->fired = mk_mem_alloc_z(sizeof(struct mk_event) * size);
    if (!ctx->fired) {
        mk_mem_free(ctx->events);
        mk_mem_free(ctx);
        return NULL;
    }
    ctx->queue_size = size;

    return ctx;
}
Exemplo n.º 2
0
static inline void *_mk_event_loop_create(int size)
{
    mk_event_ctx_t *ctx;

    /* Main event context */
    ctx = mk_mem_malloc_z(sizeof(mk_event_ctx_t));
    if (!ctx) {
        return NULL;
    }

    /* Create the epoll instance */
    ctx->efd = epoll_create1(EPOLL_CLOEXEC);
    if (ctx->efd == -1) {
        mk_libc_error("epoll_create");
        mk_mem_free(ctx);
        return NULL;
    }

    /* Allocate space for events queue */
    ctx->events = mk_mem_malloc_z(sizeof(struct epoll_event) * size);
    if (!ctx->events) {
        close(ctx->efd);
        mk_mem_free(ctx);
        return NULL;
    }
    ctx->queue_size = size;
    return ctx;
}
Exemplo n.º 3
0
void mk_cache_worker_exit()
{
    char *cache_error;

    /* Cache header request -> last modified */
    mk_ptr_free(MK_TLS_GET(mk_tls_cache_header_lm));
    mk_mem_free(MK_TLS_GET(mk_tls_cache_header_lm));

    /* Cache header request -> content length */
    mk_ptr_free(MK_TLS_GET(mk_tls_cache_header_cl));
    mk_mem_free(MK_TLS_GET(mk_tls_cache_header_cl));

    /* Cache iov header struct */
    mk_iov_free(MK_TLS_GET(mk_tls_cache_iov_header));

    /* Cache gmtime buffer */
    mk_mem_free(MK_TLS_GET(mk_tls_cache_gmtime));

    /* Cache the most used text representations of utime2gmt */
    mk_mem_free(MK_TLS_GET(mk_tls_cache_gmtext));

    /* Cache buffer for strerror_r(2) */
    cache_error = pthread_getspecific(mk_utils_error_key);
    mk_mem_free(cache_error);
}
Exemplo n.º 4
0
void mk_iov_free(struct mk_iov *mk_io)
{
    mk_iov_free_marked(mk_io);
    mk_mem_free(mk_io->buf_to_free);
    mk_mem_free(mk_io->io);
    mk_mem_free(mk_io);
}
Exemplo n.º 5
0
static inline void *_mk_event_loop_create(int size)
{
    struct mk_event_ctx *ctx;

    /* Main event context */
    ctx = mk_mem_malloc_z(sizeof(struct mk_event_ctx));
    if (!ctx) {
        return NULL;
    }

    /* Create the epoll instance */
    ctx->kfd = kqueue();
    if (ctx->kfd == -1) {
        mk_libc_error("kqueue");
        mk_mem_free(ctx);
        return NULL;
    }

    /* Allocate space for events queue */
    ctx->events = mk_mem_malloc_z(sizeof(struct kevent) * size);
    if (!ctx->events) {
        close(ctx->kfd);
        mk_mem_free(ctx);
        return NULL;
    }
    ctx->queue_size = size;
    return ctx;
}
Exemplo n.º 6
0
int mk_kernel_version()
{
    int a, b, c;
    int len;
    int pos;
    char *p, *t;
    char *tmp;
    struct utsname uts;

    if (uname(&uts) == -1) {
        mk_libc_error("uname");
    }
    len = strlen(uts.release);

    /* Fixme: this don't support Linux Kernel 10.x.x :P */
    a = (*uts.release - '0');

    /* Second number */
    p = (uts.release) + 2;
    pos = mk_string_char_search(p, '.', len - 2);
    if (pos <= 0) {
        /* Some Debian systems uses a different notation, e.g: 3.14-2-amd64 */
        pos = mk_string_char_search(p, '-', len - 2);
        if (pos <= 0) {
            return -1;
        }
    }

    tmp = mk_string_copy_substr(p, 0, pos);
    if (!tmp) {
        return -1;
    }
    b = atoi(tmp);
    mk_mem_free(tmp);

    /* Last number (it needs filtering) */
    t = p = p + pos + 1;
    do {
        t++;
    } while (isdigit(*t));

    tmp = mk_string_copy_substr(p, 0, t - p);
    if (!tmp) {
        return -1;
    }
    c = atoi(tmp);
    mk_mem_free(tmp);

    MK_TRACE("Kernel detected: %i.%i.%i", a, b, c);
    return MK_KERNEL_VERSION(a, b, c);
}
Exemplo n.º 7
0
/* Create a new loop */
struct mk_event_loop *mk_event_loop_create(int size)
{
    void *backend;
    struct mk_event_loop *loop;

    backend = _mk_event_loop_create(size);
    if (!backend) {
        return NULL;
    }

    loop = mk_mem_alloc_z(sizeof(struct mk_event_loop));
    if (!loop) {
        _mk_event_loop_destroy(backend);
        return NULL;
    }

    loop->events = mk_mem_alloc_z(sizeof(struct mk_event) * size);
    if (!loop->events) {
        _mk_event_loop_destroy(backend);
        mk_mem_free(loop);
        return NULL;
    }

    loop->size   = size;
    loop->data   = backend;

    return loop;
}
Exemplo n.º 8
0
/* Read file content to a memory buffer,
 * Use this function just for really SMALL files
 */
char *mk_file_to_buffer(const char *path)
{
    FILE *fp;
    char *buffer;
    long bytes;
    struct file_info finfo;

    if (mk_file_get_info(path, &finfo) != 0) {
        return NULL;
    }

    if (!(fp = fopen(path, "r"))) {
        return NULL;
    }

    buffer = calloc(finfo.size + 1, sizeof(char));
    if (!buffer) {
        fclose(fp);
        return NULL;
    }

    bytes = fread(buffer, finfo.size, 1, fp);

    if (bytes < 1) {
        mk_mem_free(buffer);
        fclose(fp);
        return NULL;
    }

    fclose(fp);
    return (char *) buffer;

}
Exemplo n.º 9
0
/* Create a new loop */
mk_event_loop_t *mk_event_loop_create(int size)
{
    void *backend;
    mk_event_loop_t *loop;

    backend = _mk_event_loop_create(size);
    if (!backend) {
        return NULL;
    }

    loop = mk_mem_malloc_z(sizeof(mk_event_loop_t));
    if (!loop) {
        return NULL;
    }

    loop->events = mk_mem_malloc_z(sizeof(mk_event_t) * size);
    if (!loop->events) {
        mk_mem_free(loop);
        return NULL;
    }

    loop->size   = size;
    loop->data   = backend;

    return loop;
}
Exemplo n.º 10
0
void mk_exit_all()
{
    int i;
    int n;
    uint64_t val;

    /* Distribute worker signals to stop working */
    val = MK_SCHEDULER_SIGNAL_FREE_ALL;
    for (i = 0; i < mk_config->workers; i++) {
        n = write(sched_list[i].signal_channel_w, &val, sizeof(val));
        if (n < 0) {
            perror("write");
        }
    }

    /* Wait for workers to finish */
    for (i = 0; i < mk_config->workers; i++) {
        pthread_join(sched_list[i].tid, NULL);
    }

    mk_utils_remove_pid();
    mk_plugin_exit_all();
    mk_config_free_all();
    mk_mem_free(sched_list);
    mk_clock_exit();
}
Exemplo n.º 11
0
/* when we catch a signal and want to exit we call this function
   to do it gracefully */
static void mk_signal_exit()
{
    int i;
    uint64_t val;

    /* ignore future signals to properly handle the cleanup */
    signal(SIGTERM, SIG_IGN);
    signal(SIGINT,  SIG_IGN);
    signal(SIGHUP,  SIG_IGN);

    /* Distribute worker signals to stop working */
    val = MK_SCHEDULER_SIGNAL_FREE_ALL;
    for (i = 0; i < config->workers; i++) {
        write(sched_list[i].signal_channel, &val, sizeof(val));
    }

    /* Wait for workers to finish */
    for (i = 0; i < config->workers; i++) {
        pthread_join(sched_list[i].tid, NULL);
    }

    mk_utils_remove_pid();
    mk_plugin_exit_all();
    mk_config_free_all();
    mk_mem_free(sched_list);
    mk_clock_exit();
    mk_info("Exiting... >:(");
    exit(EXIT_SUCCESS);
}
Exemplo n.º 12
0
/*
 * This routine creates a timer, since this select(2) backend aims to be used
 * in very old systems to be compatible, we cannot trust timerfd_create(2)
 * will be available (e.g: Cygwin), so our workaround is to create a pipe(2)
 * and a thread, this thread writes a byte upon the expiration time is reached.
 */
static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
                                           time_t sec, long nsec, void *data)
{
    int ret;
    int fd[2];
    struct mk_event *event;
    struct fd_timer *timer;

    timer = mk_mem_alloc(sizeof(struct fd_timer));
    if (!timer) {
        return -1;
    }

    ret = pipe(fd);
    if (ret < 0) {
        mk_mem_free(timer);
        mk_libc_error("pipe");
        return ret;
    }

    event = (struct mk_event *) data;
    event->fd = fd[0];
    event->type = MK_EVENT_NOTIFICATION;
    event->mask = MK_EVENT_EMPTY;

    _mk_event_add(ctx, fd[0], MK_EVENT_NOTIFICATION, MK_EVENT_READ, data);
    event->mask = MK_EVENT_READ;

    /* Compose the timer context, this is released inside the worker thread */
    timer->fd   = fd[1];
    timer->sec  = sec;
    timer->nsec = nsec;
    timer->run  = MK_TRUE;

    event->data = timer;

    /* Now the dirty workaround, create a thread */
    ret = mk_utils_worker_spawn(_timeout_worker, timer, &timer->tid);
    if (ret < 0) {
        close(fd[0]);
        close(fd[1]);
        mk_mem_free(timer);
        return -1;
    }

    return fd[0];
}
Exemplo n.º 13
0
/*
 * @METHOD_NAME: chan_free
 * @METHOD_DESC: release a given channel.
 * @METHOD_PROTO: void chan_free(mk_thread_channel_t *chan)
 * @METHOD_PARAM: chan the target channel to be released.
 * @METHOD_RETURN: this method do not return any value.
 */
void mk_thread_channel_free(struct mk_thread_channel *chan)
{
    assert(chan);
    if (chan->receiver != -1) {
        mk_list_del(&chan->_head);
    }
    mk_mem_free(chan);
}
Exemplo n.º 14
0
int mk_user_init(struct client_session *cs, struct session_request *sr)
{
    int limit;
    const int offset = 2; /* The user is defined after the '/~' string, so offset = 2 */
    const int user_len = 255;
    char user[user_len], *user_uri;
    struct passwd *s_user;

    if (sr->uri_processed.len <= 2) {
        return -1;
    }

    limit = mk_string_char_search(sr->uri_processed.data + offset, '/',
                                  sr->uri_processed.len);

    if (limit == -1) {
        limit = (sr->uri_processed.len) - offset;
    }

    if (limit + offset >= (user_len)) {
        return -1;
    }

    memcpy(user, sr->uri_processed.data + offset, limit);
    user[limit] = '\0';

    MK_TRACE("user: '******'", user);

    /* Check system user */
    if ((s_user = getpwnam(user)) == NULL) {
        mk_request_error(MK_CLIENT_NOT_FOUND, cs, sr);
        return -1;
    }

    if (sr->uri_processed.len > (unsigned int) (offset+limit)) {
        user_uri = mk_mem_malloc(sr->uri_processed.len);
        if (!user_uri) {
            return -1;
        }

        memcpy(user_uri,
               sr->uri_processed.data + (offset + limit),
               sr->uri_processed.len - offset - limit);
        user_uri[sr->uri_processed.len - offset - limit] = '\0';

        mk_string_build(&sr->real_path.data, &sr->real_path.len,
                        "%s/%s%s", s_user->pw_dir, config->user_dir, user_uri);
        mk_mem_free(user_uri);
    }
    else {
        mk_string_build(&sr->real_path.data, &sr->real_path.len,
                        "%s/%s", s_user->pw_dir, config->user_dir);
    }

    sr->user_home = MK_TRUE;
    return 0;
}
Exemplo n.º 15
0
int mk_socket_connect(char *host, int port, int async)
{
    int ret;
    int socket_fd = -1;
    char *port_str = 0;
    unsigned long len;
    struct addrinfo hints;
    struct addrinfo *res, *rp;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    mk_string_build(&port_str, &len, "%d", port);

    ret = getaddrinfo(host, port_str, &hints, &res);
    mk_mem_free(port_str);
    if(ret != 0) {
        mk_err("Can't get addr info: %s", gai_strerror(ret));
        return -1;
    }
    for (rp = res; rp != NULL; rp = rp->ai_next) {
        socket_fd = mk_socket_create(rp->ai_family,
                                     rp->ai_socktype, rp->ai_protocol);

        if (socket_fd == -1) {
            mk_warn("Error creating client socket, retrying");
            continue;
        }

        if (async == MK_TRUE) {
            mk_socket_set_nonblocking(socket_fd);
        }

        ret = connect(socket_fd,
                      (struct sockaddr *) rp->ai_addr, rp->ai_addrlen);
        if (ret == -1) {
            if (errno == EINPROGRESS) {
                break;
            }
            else {
                printf("%s", strerror(errno));
                perror("connect");
                exit(1);
                close(socket_fd);
                continue;
            }
        }
        break;
    }
    freeaddrinfo(res);

    if (rp == NULL)
        return -1;

    return socket_fd;
}
Exemplo n.º 16
0
static void mk_request_free(struct session_request *sr)
{
    if (sr->fd_file > 0) {
        close(sr->fd_file);
    }

    if (sr->headers.location) {
        mk_mem_free(sr->headers.location);
    }

    if (sr->uri_processed.data != sr->uri.data) {
        mk_pointer_free(&sr->uri_processed);
    }

    mk_mem_free(sr->virtual_user);

    if (sr->real_path.data != sr->real_path_static) {
        mk_pointer_free(&sr->real_path);
    }
}
Exemplo n.º 17
0
void mk_server_listen_free()
{
    struct mk_list *tmp;
    struct mk_list *head;
    struct mk_server_listen *listener;

    mk_list_foreach_safe(head, tmp, server_listen) {
        listener = mk_list_entry(head, struct mk_server_listen, _head);
        mk_list_del(&listener->_head);
        mk_mem_free(listener);
    }
Exemplo n.º 18
0
/* Read dirhtml config and themes */
int mk_dirhtml_conf(char *confdir)
{
    int ret = 0;
    unsigned long len;
    char *conf_file = NULL;

    mk_api->str_build(&conf_file, &len, "%s", confdir);

    /* Read configuration */
    ret = mk_dirhtml_read_config(conf_file);
    if (ret < 0) {
        mk_mem_free(conf_file);
        return -1;
    }

    /*
     * This function will load the default theme setted in dirhtml_conf struct
     */
    mk_mem_free(conf_file);
    return mk_dirhtml_theme_load();
}
Exemplo n.º 19
0
/* Write Monkey's PID */
int mk_utils_register_pid()
{
    int fd;
    char pidstr[MK_MAX_PID_LEN];
    unsigned long len = 0;
    char *filepath = NULL;
    struct flock lock;
    struct stat sb;
    struct mk_config_listener *listen;

    if (config->pid_status == MK_TRUE)
        return -1;

    listen = mk_list_entry_first(&config->listeners,
                                 struct mk_config_listener, _head);
    mk_string_build(&filepath, &len, "%s.%s",
                    config->pid_file_path,
                    listen->port);
    if (!stat(filepath, &sb)) {
        /* file exists, perhaps previously kepts by SIGKILL */
        unlink(filepath);
    }

    if ((fd = open(filepath, O_WRONLY | O_CREAT | O_CLOEXEC, 0444)) < 0) {
        mk_err("Error: I can't log pid of monkey");
        exit(EXIT_FAILURE);
    }

    /* create a write exclusive lock for the entire file */
    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;

    if (fcntl(fd, F_SETLK, &lock) < 0) {
        close(fd);
        mk_err("Error: I cannot set the lock for the pid of monkey");
        exit(EXIT_FAILURE);
    }

    sprintf(pidstr, "%i", getpid());
    ssize_t write_len = strlen(pidstr);
    if (write(fd, pidstr, write_len) != write_len) {
        close(fd);
        mk_err("Error: I cannot write the lock for the pid of monkey");
        exit(EXIT_FAILURE);
    }

    mk_mem_free(filepath);
    config->pid_status = MK_TRUE;

    return 0;
}
Exemplo n.º 20
0
static inline void *_mk_event_loop_create(int size)
{
    int efd;
    struct mk_event_ctx *ctx;

    /* Main event context */
    ctx = mk_mem_alloc_z(sizeof(struct mk_event_ctx));
    if (!ctx) {
        return NULL;
    }

    /* Create the epoll instance */
 #ifdef EPOLL_CLOEXEC
    efd = epoll_create1(EPOLL_CLOEXEC);
 #else
    efd = epoll_create(1);
    if (efd > 0) {
        if (fcntl(efd, F_SETFD, FD_CLOEXEC) == -1) {
            perror("fcntl");
        }
    }
 #endif

    if (efd == -1) {
        mk_libc_error("epoll_create");
        mk_mem_free(ctx);
        return NULL;
    }
    ctx->efd = efd;

    /* Allocate space for events queue */
    ctx->events = mk_mem_alloc_z(sizeof(struct epoll_event) * size);
    if (!ctx->events) {
        close(ctx->efd);
        mk_mem_free(ctx);
        return NULL;
    }
    ctx->queue_size = size;
    return ctx;
}
Exemplo n.º 21
0
void mk_server_listen_free()
{
    struct mk_list *list;
    struct mk_list *tmp;
    struct mk_list *head;
    struct mk_server_listen *listener;

    list = MK_TLS_GET(mk_tls_server_listen);
    mk_list_foreach_safe(head, tmp, list) {
        listener = mk_list_entry(head, struct mk_server_listen, _head);
        mk_list_del(&listener->_head);
        mk_mem_free(listener);
    }
Exemplo n.º 22
0
/* Remove PID file */
int mk_utils_remove_pid()
{
    unsigned long len = 0;
    char *filepath = NULL;

    mk_string_build(&filepath, &len, "%s.%d", config->pid_file_path, config->serverport);
    mk_user_undo_uidgid();
    if (unlink(filepath)) {
        mk_warn("cannot delete pidfile\n");
    }
    mk_mem_free(filepath);
    config->pid_status = MK_FALSE;
    return 0;
}
Exemplo n.º 23
0
int mk_iov_realloc(struct mk_iov *mk_io, int new_size)
{
    void **new_buf;
    struct iovec *new_io;

    new_io  = mk_mem_realloc(mk_io->io, sizeof(struct iovec) * new_size) ;
    new_buf = mk_mem_realloc(mk_io->buf_to_free, sizeof(void *) * new_size);

    if (!new_io || !new_buf) {
        MK_TRACE("could not reallocate IOV");
        mk_mem_free(new_io);
        mk_mem_free(new_buf);
        return -1;
    }

    /* update data */
    mk_io->io = new_io;
    mk_io->buf_to_free = new_buf;

    mk_io->size = new_size;

    return 0;
}
Exemplo n.º 24
0
/*
 * @METHOD_NAME: printf
 * @METHOD_DESC: It format and enqueue a buffer of data to be send to the HTTP client as response body.
 * The buffer allocated is freed internally when the data is flushed completely.
 * @METHOD_PARAM: dr the request context information hold by a duda_request_t type
 * @METHOD_PARAM: format Specifies the subsequent arguments to be formatted
 * @METHOD_RETURN: Upon successful completion it returns 0, on error returns -1.
 */
int duda_response_printf(duda_request_t *dr, const char *format, ...)
{
    int ret;
    int n, size = 128;
    char *p, *np;
    va_list ap;

    if ((p = mk_mem_alloc(size)) == NULL) {
        return -1;
    }

    /* Try to print in the allocated space. */
    while (1) {
        va_start(ap, format);
        n = vsnprintf(p, size, format, ap);

        /* If that worked, return the string. */
        if (n > -1 && n < size)
            break;

        size *= 2;  /* twice the old size */
        if ((np = mk_mem_realloc(p, size)) == NULL) {
            mk_mem_free(p);
            return - 1;
        } else {
            p = np;
        }
    }
    va_end(ap);

    ret = _print(dr, p, n, MK_TRUE);
    if (ret == -1) {
        mk_mem_free(p);
    }

    return ret;
}
Exemplo n.º 25
0
/* If the URI contains hexa format characters it will return
 * convert the Hexa values to ASCII character
 */
char *mk_utils_url_decode(mk_ptr_t uri)
{
    int tmp, hex_result;
    unsigned int i;
    int buf_idx = 0;
    char *buf;
    char hex[3];

    if ((tmp = mk_string_char_search(uri.data, '%', uri.len)) < 0) {
        return NULL;
    }

    i = tmp;

    buf = mk_mem_malloc_z(uri.len);

    if (i > 0) {
        strncpy(buf, uri.data, i);
        buf_idx = i;
    }

    while (i < uri.len) {
        if (uri.data[i] == '%' && i + 2 < uri.len) {
            memset(hex, '\0', sizeof(hex));
            strncpy(hex, uri.data + i + 1, 2);
            hex[2] = '\0';

            hex_result = mk_utils_hex2int(hex, 2);

            if (hex_result != -1) {
                buf[buf_idx] = hex_result;
            }
            else {
                mk_mem_free(buf);
                return NULL;
            }
            i += 2;
        }
        else {
            buf[buf_idx] = uri.data[i];
        }
        i++;
        buf_idx++;
    }
    buf[buf_idx] = '\0';

    return buf;
}
Exemplo n.º 26
0
int mk_socket_connect(char *host, int port)
{
    int ret;
    int socket_fd = -1;
    char *port_str = 0;
    unsigned long len;
    struct addrinfo hints;
    struct addrinfo *res, *rp;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    mk_string_build(&port_str, &len, "%d", port);

    ret = getaddrinfo(host, port_str, &hints, &res);
    mk_mem_free(port_str);
    if(ret != 0) {
        mk_err("Can't get addr info: %s", gai_strerror(ret));
        return -1;
    }
    for (rp = res; rp != NULL; rp = rp->ai_next) {
        socket_fd = mk_socket_create(rp->ai_family,
                                     rp->ai_socktype, rp->ai_protocol);

        if( socket_fd == -1) {
            mk_warn("Error creating client socket, retrying");
            continue;
        }

        if (connect(socket_fd,
                    (struct sockaddr *) rp->ai_addr, rp->ai_addrlen) == -1) {
            close(socket_fd);
            continue;
        }

        break;
    }
    freeaddrinfo(res);

    if (rp == NULL)
        return -1;

    return socket_fd;
}
Exemplo n.º 27
0
void mk_request_free(struct session_request *sr)
{
    if (sr->fd_file > 0) {
        mk_vhost_close(sr);
    }

    if (sr->headers.location) {
        mk_mem_free(sr->headers.location);
    }

    if (sr->uri_processed.data != sr->uri.data) {
        mk_ptr_free(&sr->uri_processed);
    }

    if (sr->real_path.data != sr->real_path_static) {
        mk_ptr_free(&sr->real_path);
    }
}
Exemplo n.º 28
0
int mk_socket_tcp_autocorking()
{
    int ret = MK_FALSE;
    char *buf;

    buf = mk_file_to_buffer(TCP_CORKING_PATH);
    if (!buf) {
        ret = MK_FALSE;
    }
    else {
        if (strncmp(buf, "1", 1) == 0) {
            ret = MK_TRUE;
        }
        mk_mem_free(buf);
    }

    return ret;
}
Exemplo n.º 29
0
long int mk_method_validate_content_length(const char *body, int body_len)
{
    struct headers_toc toc;
    long int len;
    mk_pointer tmp;

    /* 
     * obs: Table of Content (toc) is created when the full
     * request has arrived, this function cannot be used from
     * mk_http_pending_request().
     */
    mk_request_header_toc_parse(&toc, body, body_len);
    tmp = mk_request_header_get(&toc, 
                                mk_rh_content_length.data,
                                mk_rh_content_length.len);

    if (!tmp.data) {
        int pos_header;
        int pos_crlf;
        char *str_cl;

        /* Pre-parsing mode: Check if content-length was sent */
        pos_header = mk_string_search(body, RH_CONTENT_LENGTH, MK_STR_INSENSITIVE);
        if (pos_header <= 0) {
            return -1;
        }

        pos_crlf = mk_string_search(body + pos_header, MK_IOV_CRLF, MK_STR_SENSITIVE);
        if (pos_crlf <= 0) {
            return -1;
        }

        str_cl = mk_string_copy_substr(body + pos_header + mk_rh_content_length.len + 1,
                                       0, pos_header + pos_crlf);
        len = strtol(str_cl, (char **) NULL, 10);
        mk_mem_free(str_cl);

        return len;
    }

    len = strtol(tmp.data, (char **) NULL, 10);

    return len;
}
Exemplo n.º 30
0
/* Remove PID file */
int mk_utils_remove_pid()
{
    unsigned long len = 0;
    char *filepath = NULL;
    struct mk_config_listener *listen;

    listen = mk_list_entry_first(&config->listeners,
                                 struct mk_config_listener, _head);
    mk_string_build(&filepath, &len, "%s.%s",
                    config->pid_file_path,
                    listen->port);
    mk_user_undo_uidgid();
    if (unlink(filepath)) {
        mk_warn("cannot delete pidfile\n");
    }
    mk_mem_free(filepath);
    config->pid_status = MK_FALSE;
    return 0;
}