1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37 #include <sys/types.h>
38 #include <sys/socket.h>
43 #include <sys/sendfile.h>
56 int SendFile(int socket
, struct client_request
*cr
, struct request
*sr
)
60 nbytes
= sendfile(socket
, sr
->fd_file
, &sr
->bytes_offset
,
63 if (nbytes
> 0 && sr
->loop
== 0) {
64 mk_socket_set_cork_flag(socket
, TCP_CORK_OFF
);
68 fprintf(stderr
, "error from sendfile: %s\n", strerror(errno
));
72 sr
->bytes_to_send
-= nbytes
;
76 return sr
->bytes_to_send
;
79 /* Devuelve la fecha para enviarla
81 mk_pointer
PutDate_string(time_t date
)
87 mk_pointer_reset(&date_gmt
);
90 if ((date
= time(NULL
)) == -1) {
95 date_gmt
.data
= mk_mem_malloc(size
);
96 gmt_tm
= (struct tm
*) gmtime(&date
);
97 n
= strftime(date_gmt
.data
, size
- 1, GMT_DATEFORMAT
, gmt_tm
);
98 date_gmt
.data
[n
] = '\0';
104 time_t PutDate_unix(char *date
)
106 time_t new_unix_time
;
109 if (!strptime(date
, GMT_DATEFORMAT
, (struct tm
*) &t_data
)) {
113 new_unix_time
= mktime((struct tm
*) &t_data
);
115 return (new_unix_time
);
118 int mk_buffer_cat(mk_pointer
* p
, char *buf1
, char *buf2
)
127 p
->data
= (char *) mk_mem_malloc(len1
+ len2
+ 1);
130 memcpy(p
->data
, buf1
, len1
);
131 memcpy(p
->data
+ len1
, buf2
, len2
);
132 p
->data
[len1
+ len2
] = '\0';
135 p
->len
= len1
+ len2
;
140 char *m_build_buffer(char **buffer
, unsigned long *len
, const char *format
,
146 static size_t _mem_alloc
= 64;
149 /* *buffer *must* be an empty/NULL buffer */
151 *buffer
= (char *) mk_mem_malloc(_mem_alloc
);
157 va_start(ap
, format
);
158 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
160 if (length
>= alloc
) {
161 ptr
= realloc(*buffer
, length
+ 1);
168 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
183 /* Run current process in background mode (daemon, evil Monkey >:) */
184 int mk_utils_set_daemon()
193 exit(0); /* Success */
196 setsid(); /* Create new session */
197 fclose(stdin
); /* close screen outputs */
204 /* If the URI contains hexa format characters it will return
205 * convert the Hexa values to ASCII character
207 char *mk_utils_hexuri_to_ascii(mk_pointer uri
)
210 int i
, hex_result
, aux_char
;
215 if ((i
= mk_string_char_search(uri
.data
, '%', uri
.len
)) < 0) {
219 buf
= mk_mem_malloc_z(uri
.len
);
223 strncpy(buf
, uri
.data
, i
);
227 while (i
< uri
.len
) {
228 if (uri
.data
[i
] == '%' && i
+ 2 < uri
.len
) {
229 memset(hex
, '\0', sizeof(hex
));
230 strncpy(hex
, uri
.data
+ i
+ 1, 2);
233 if ((hex_result
= hex2int(hex
)) <= 127) {
234 buf
[buf_idx
] = toascii(hex_result
);
237 if ((aux_char
= get_char(hex_result
)) != -1) {
238 buf
[buf_idx
] = aux_char
;
248 buf
[buf_idx
] = uri
.data
[i
];
258 mk_pointer
mk_utils_int2mkp(int n
)
264 buf
= mk_mem_malloc(MK_UTILS_INT2MKP_BUFFER_LEN
);
265 len
= snprintf(buf
, MK_UTILS_INT2MKP_BUFFER_LEN
, "%i\r\n", n
);
274 #include <sys/time.h>
275 void mk_utils_trace(const char *component
, int color
, const char *function
,
276 char *file
, int line
, const char* format
, ...)
279 char *color_function
= NULL
;
280 char *color_fileline
= NULL
;
285 gettimeofday(&tv
, &tz
);
287 /* Switch message color */
290 color_function
= ANSI_YELLOW
;
291 color_fileline
= ANSI_WHITE
;
293 case MK_TRACE_PLUGIN
:
294 color_function
= ANSI_BLUE
;
295 color_fileline
= ANSI_WHITE
;
299 va_start( args
, format
);
301 fprintf(stderr
, "~ %s%2i.%i%s %s%s[%s%s%s%s%s|%s:%i%s] %s%s():%s ",
302 ANSI_CYAN
, (int) (tv
.tv_sec
- monkey_init_time
), (int) tv
.tv_usec
, ANSI_RESET
,
303 ANSI_MAGENTA
, ANSI_BOLD
,
304 ANSI_RESET
, ANSI_BOLD
, ANSI_GREEN
, component
, color_fileline
, file
,
306 color_function
, function
, ANSI_RED
);
307 vfprintf( stderr
, format
, args
);
309 fprintf( stderr
, "%s\n", ANSI_RESET
);