Fix #29: add ico image/x-icon to mime type to support favicon.ico. Removed duplicate...
[MonkeyD.git] / src / utils.c
blob3d4b155972ec3ccabcc1d880f8e850071208b122
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-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>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <limits.h>
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <signal.h>
43 #include <sys/sendfile.h>
45 #include <time.h>
47 #include "monkey.h"
48 #include "memory.h"
49 #include "utils.h"
50 #include "str.h"
51 #include "config.h"
52 #include "chars.h"
53 #include "socket.h"
54 #include "clock.h"
56 int SendFile(int socket, struct request *sr)
58 long int nbytes=0;
60 nbytes = sendfile(socket, sr->fd_file, &sr->bytes_offset,
61 sr->bytes_to_send);
63 if (nbytes == -1) {
64 fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
65 return -1;
67 else
69 sr->bytes_to_send-=nbytes;
71 return sr->bytes_to_send;
74 /* It's a valid directory ? */
75 int CheckDir(char *pathfile)
77 struct stat path;
79 if(stat(pathfile,&path)==-1)
80 return -1;
82 if(!(path.st_mode & S_IFDIR))
83 return -1;
85 return 0;
88 int CheckFile(char *pathfile)
90 struct stat path;
92 if(stat(pathfile,&path)==-1)
93 return -1;
95 if(!(path.st_mode & S_IFREG))
96 return -1;
98 return 0;
101 /* Devuelve la fecha para enviarla
102 en el header */
103 mk_pointer PutDate_string(time_t date)
105 int n, size=50;
106 mk_pointer date_gmt;
107 struct tm *gmt_tm;
109 mk_pointer_reset(&date_gmt);
111 if(date==0){
112 if ( (date = time(NULL)) == -1 ){
113 return date_gmt;
117 date_gmt.data = mk_mem_malloc(size);
118 gmt_tm = (struct tm *) gmtime(&date);
119 n = strftime(date_gmt.data, size-1, GMT_DATEFORMAT, gmt_tm);
120 date_gmt.data[n] = '\0';
121 date_gmt.len = n;
123 return date_gmt;
126 time_t PutDate_unix(char *date)
128 time_t new_unix_time;
129 struct tm t_data;
131 if(!strptime(date, GMT_DATEFORMAT, (struct tm *) &t_data)){
132 return -1;
135 new_unix_time = mktime((struct tm *) &t_data);
137 return (new_unix_time);
140 int mk_buffer_cat(mk_pointer *p, char *buf1, char *buf2){
142 int len1, len2;
144 len1 = strlen(buf1);
145 len2 = strlen(buf2);
147 /* alloc space */
148 p->data = (char *) mk_mem_malloc(len1+len2+1);
150 /* copy data */
151 memcpy(p->data, buf1, len1);
152 memcpy(p->data+len1, buf2, len2);
153 p->data[len1+len2]='\0';
155 /* assign len */
156 p->len = len1+len2;
158 return 0;
161 char *m_build_buffer(char **buffer, unsigned long *len, const char *format, ...)
163 va_list ap;
164 int length;
165 char *ptr;
166 static size_t _mem_alloc = 64;
167 size_t alloc = 0;
169 /* *buffer *must* be an empty/NULL buffer */
171 *buffer = (char *) mk_mem_malloc(_mem_alloc);
172 if(!*buffer)
174 return NULL;
176 alloc = _mem_alloc;
178 va_start(ap, format);
179 length = vsnprintf(*buffer, alloc, format, ap);
181 if(length >= alloc) {
182 ptr = realloc(*buffer, length + 1);
183 if(!ptr) {
184 va_end(ap);
185 return NULL;
187 *buffer = ptr;
188 alloc = length + 1;
189 length = vsnprintf(*buffer, alloc, format, ap);
191 va_end(ap);
193 if(length<0){
194 return NULL;
197 ptr = *buffer;
198 ptr[length] = '\0';
199 *len = length;
201 return *buffer;
204 /* Run current process in background mode (daemon, evil Monkey >:) */
205 int mk_utils_set_daemon()
207 switch (fork())
209 case 0 : break;
210 case -1: exit(1); break; /* Error */
211 default: exit(0); /* Success */
214 setsid(); /* Create new session */
215 fclose(stdin); /* close screen outputs */
216 fclose(stderr);
217 fclose(stdout);
219 return 0;
223 char *get_real_string(mk_pointer uri){
225 int i, hex_result, aux_char;
226 int buf_idx=0;
227 char *buf;
228 char hex[3];
230 if((i = mk_string_char_search(uri.data, '%', uri.len))<0)
232 return NULL;
235 buf = mk_mem_malloc_z(uri.len);
238 if(i>0){
239 strncpy(buf, uri.data, i);
240 buf_idx = i;
243 while(i<uri.len)
245 if(uri.data[i]=='%' && i+2<uri.len){
246 memset(hex, '\0', sizeof(hex));
247 strncpy(hex, uri.data+i+1,2);
248 hex[2]='\0';
250 if((hex_result=hex2int(hex))<=127){
251 buf[buf_idx]=toascii(hex_result);
253 else {
254 if((aux_char=get_char(hex_result))!=-1){
255 buf[buf_idx]=aux_char;
257 else{
258 mk_mem_free(buf);
259 return NULL;
262 i+=2;
264 else{
265 buf[buf_idx] = uri.data[i];
267 i++;
268 buf_idx++;
270 buf[buf_idx]='\0';
272 return (char *) buf;
275 void mk_utils_toupper(char *string)
277 int i, len;
279 len = strlen(string);
280 for(i=0; i<len; i++)
282 string[i] = toupper(string[i]);
286 mk_pointer mk_utils_int2mkp(int n)
288 mk_pointer p;
289 char *buf;
290 unsigned long len;
291 int size = 32;
293 buf = mk_mem_malloc(size);
294 len = snprintf(buf, 32, "%i", n);
296 p.data = buf;
297 p.len = len;
299 return p;