Fix #29: add ico image/x-icon to mime type to support favicon.ico. Removed duplicate...
[MonkeyD.git] / src / iov.c
blobe2bd2f54cbd06420ed8652558bed0420a00fcd51
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2008, Eduardo Silva P.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #define _GNU_SOURCE
23 #include <fcntl.h>
25 #include <sys/uio.h>
26 #include <sys/mman.h>
27 #include <errno.h>
29 #include <stdio.h>
31 #include "monkey.h"
33 #include "header.h"
34 #include "memory.h"
35 #include "iov.h"
37 struct mk_iov *mk_iov_create(int n, int offset)
39 struct mk_iov *iov;
41 iov = mk_mem_malloc(sizeof(struct mk_iov));
42 iov->iov_idx = offset;
43 iov->io = mk_mem_malloc(n*sizeof(struct iovec));
44 iov->buf_to_free = mk_mem_malloc(n*sizeof(char *));
45 iov->buf_idx = 0;
46 iov->total_len = 0;
47 iov->size = n;
49 return iov;
52 int mk_iov_add_entry(struct mk_iov *mk_io, char *buf, int len,
53 mk_pointer sep, int free)
55 mk_io->io[mk_io->iov_idx].iov_base = buf;
56 mk_io->io[mk_io->iov_idx].iov_len = len;
57 mk_io->iov_idx++;
58 mk_io->total_len += len;
60 #ifdef DEBUG_IOV
61 if(mk_io->iov_idx > mk_io->size){
62 printf("\nDEBUG IOV :: ERROR, Broke array size in:");
63 printf("\n '''%s'''", buf);
64 fflush(stdout);
66 #endif
68 /* Add separator */
69 if(sep.len > 0)
71 mk_io->io[mk_io->iov_idx].iov_base = sep.data;
72 mk_io->io[mk_io->iov_idx].iov_len = sep.len;
73 mk_io->iov_idx++;
74 mk_io->total_len += sep.len;
77 if(free == MK_IOV_FREE_BUF)
79 _mk_iov_set_free(mk_io, buf);
82 return mk_io->iov_idx;
85 int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
86 int free, int idx)
88 mk_io->io[idx].iov_base = buf;
89 mk_io->io[idx].iov_len = len;
90 mk_io->total_len += len;
92 if(free == MK_IOV_FREE_BUF)
94 _mk_iov_set_free(mk_io, buf);
97 return 0;
100 void _mk_iov_set_free(struct mk_iov *mk_io, char *buf)
102 mk_io->buf_to_free[mk_io->buf_idx] = (char *)buf;
103 mk_io->buf_idx++;
106 ssize_t mk_iov_send(int fd, struct mk_iov *mk_io, int to)
108 ssize_t n=-1;
110 if(to==MK_IOV_SEND_TO_SOCKET){
111 n = writev(fd, mk_io->io, mk_io->iov_idx);
112 if(n<0){
113 perror("writev");
116 else if(to==MK_IOV_SEND_TO_PIPE){
117 /* for some reason, vmsplice is not working as expected for us,
118 * maybe we need to fix something here, at the moment
119 * we will keep using writev to push the iovec struct to the pipe
121 * n = vmsplice(fd, mk_io->io, mk_io->iov_idx,
122 * SPLICE_F_GIFT);
124 * if(n<0){
125 * perror("vmsplice");
128 n = writev(fd, mk_io->io, mk_io->iov_idx);
130 if(n<0){
131 perror("writev");
135 return n;
138 void mk_iov_free(struct mk_iov *mk_io)
140 mk_iov_free_marked(mk_io);
141 mk_mem_free(mk_io->io);
142 mk_mem_free(mk_io);
144 void mk_iov_free_marked(struct mk_iov *mk_io)
146 int i, limit=0;
148 limit = mk_io->buf_idx;
150 for(i=0; i<limit; i++)
153 #ifdef DEBUG_IOV
154 printf("\nDEBUG IOV :: going free (idx: %i/%i): %s",i,
155 limit, mk_io->buf_to_free[i]);
156 fflush(stdout);
157 #endif
158 mk_mem_free(mk_io->buf_to_free[i]);
161 mk_io->iov_idx = 0;
162 mk_io->buf_idx = 0;
165 void mk_iov_print(struct mk_iov *mk_io)
167 int i;
169 for(i=0; i<mk_io->iov_idx; i++){
170 printf("\n%i len=%i) '%s'", i, mk_io->io[i].iov_len,
171 (char *) mk_io->io[i].iov_base);
172 fflush(stdout);
176 void mk_iov_separators_init()
178 mk_pointer_set(&mk_iov_crlf, MK_IOV_CRLF);
179 mk_pointer_set(&mk_iov_lf, MK_IOV_LF);
180 mk_pointer_set(&mk_iov_space, MK_IOV_SPACE);
181 mk_pointer_set(&mk_iov_header_value, MK_IOV_HEADER_VALUE);
182 mk_pointer_set(&mk_iov_slash, MK_IOV_SLASH);
183 mk_pointer_set(&mk_iov_none, MK_IOV_NONE);
184 mk_pointer_set(&mk_iov_equal, MK_IOV_EQUAL);