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
, int len1
, char *buf2
, int len2
)
121 p
->data
= (char *) mk_mem_malloc(len1
+ len2
+ 1);
124 memcpy(p
->data
, buf1
, len1
);
125 memcpy(p
->data
+ len1
, buf2
, len2
);
126 p
->data
[len1
+ len2
] = '\0';
129 p
->len
= len1
+ len2
;
134 char *m_build_buffer(char **buffer
, unsigned long *len
, const char *format
,
140 static size_t _mem_alloc
= 64;
143 /* *buffer *must* be an empty/NULL buffer */
145 *buffer
= (char *) mk_mem_malloc(_mem_alloc
);
151 va_start(ap
, format
);
152 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
154 if (length
>= alloc
) {
155 ptr
= realloc(*buffer
, length
+ 1);
162 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
177 /* Run current process in background mode (daemon, evil Monkey >:) */
178 int mk_utils_set_daemon()
187 exit(0); /* Success */
190 setsid(); /* Create new session */
191 fclose(stdin
); /* close screen outputs */
198 /* If the URI contains hexa format characters it will return
199 * convert the Hexa values to ASCII character
201 char *mk_utils_hexuri_to_ascii(mk_pointer uri
)
204 int i
, hex_result
, aux_char
;
209 if ((i
= mk_string_char_search(uri
.data
, '%', uri
.len
)) < 0) {
213 buf
= mk_mem_malloc_z(uri
.len
);
217 strncpy(buf
, uri
.data
, i
);
221 while (i
< uri
.len
) {
222 if (uri
.data
[i
] == '%' && i
+ 2 < uri
.len
) {
223 memset(hex
, '\0', sizeof(hex
));
224 strncpy(hex
, uri
.data
+ i
+ 1, 2);
227 if ((hex_result
= hex2int(hex
)) <= 127) {
228 buf
[buf_idx
] = toascii(hex_result
);
231 if ((aux_char
= get_char(hex_result
)) != -1) {
232 buf
[buf_idx
] = aux_char
;
242 buf
[buf_idx
] = uri
.data
[i
];
252 mk_pointer
mk_utils_int2mkp(int n
)
258 buf
= mk_mem_malloc(MK_UTILS_INT2MKP_BUFFER_LEN
);
259 len
= snprintf(buf
, MK_UTILS_INT2MKP_BUFFER_LEN
, "%i\r\n", n
);
268 #include <sys/time.h>
269 void mk_utils_trace(const char *component
, int color
, const char *function
,
270 char *file
, int line
, const char* format
, ...)
273 char *color_function
= NULL
;
274 char *color_fileline
= NULL
;
275 char *envtrace
= NULL
;
280 envtrace
= getenv("MONKEY_TRACE");
283 if (!strstr(envtrace
, file
)) {
288 gettimeofday(&tv
, &tz
);
290 /* Switch message color */
293 color_function
= ANSI_YELLOW
;
294 color_fileline
= ANSI_WHITE
;
296 case MK_TRACE_PLUGIN
:
297 color_function
= ANSI_BLUE
;
298 color_fileline
= ANSI_WHITE
;
302 va_start( args
, format
);
304 fprintf(stderr
, "~ %s%2i.%i%s %s%s[%s%s%s%s%s|%s:%i%s] %s%s():%s ",
305 ANSI_CYAN
, (int) (tv
.tv_sec
- monkey_init_time
), (int) tv
.tv_usec
, ANSI_RESET
,
306 ANSI_MAGENTA
, ANSI_BOLD
,
307 ANSI_RESET
, ANSI_BOLD
, ANSI_GREEN
, component
, color_fileline
, file
,
309 color_function
, function
, ANSI_RED
);
310 vfprintf( stderr
, format
, args
);
312 fprintf( stderr
, "%s\n", ANSI_RESET
);