static void mk_utils_gmt_cache_add(char *data, time_t time) { unsigned int i, min = 0; struct mk_gmt_cache *gcache = mk_cache_get(mk_cache_utils_gmt_text); for (i = 1; i < MK_GMT_CACHES; i++) { if (gcache[i].hits < gcache[min].hits) min = i; } gcache[min].hits = 1; gcache[min].time = time; memcpy(gcache[min].text, data, 32); }
static int mk_utils_gmt_cache_get(char **data, time_t date) { unsigned int i; struct mk_gmt_cache *gcache = mk_cache_get(mk_cache_utils_gmt_text); for (i = 0; i < MK_GMT_CACHES; i++) { if (date == gcache[i].time) { memcpy(*data, gcache[i].text, 32); gcache[i].hits++; return MK_TRUE; } } return MK_FALSE; }
/* *This function given a unix time, set in a mk_ptr_t * the date in the RFC1123 format like: * * Wed, 23 Jun 2010 22:32:01 GMT * * it also adds a 'CRLF' at the end */ int mk_utils_utime2gmt(char **data, time_t date) { const int size = 31; unsigned short year, mday, hour, min, sec; char *buf=0; struct tm *gtm; if (date == 0) { if ((date = time(NULL)) == -1) { return -1; } } else { /* Maybe it's converted already? */ if (mk_utils_gmt_cache_get(data, date) == MK_TRUE) { return size; } } /* Convert unix time to struct tm */ gtm = mk_cache_get(mk_cache_utils_gmtime); /* If this function was invoked from a non-thread context it should exit */ mk_bug(!gtm); gtm = gmtime_r(&date, gtm); if (!gtm) { return -1; } /* struct tm -> tm_year counts number of years after 1900 */ year = gtm->tm_year + 1900; /* Signed division is slow, by using unsigned we gain 25% speed */ mday = gtm->tm_mday; hour = gtm->tm_hour; min = gtm->tm_min; sec = gtm->tm_sec; /* Compose template */ buf = *data; /* Week day */ memcpy(buf, mk_date_wd[gtm->tm_wday], 5); buf += 5; /* Day of the month */ *buf++ = ('0' + (mday / 10)); *buf++ = ('0' + (mday % 10)); *buf++ = ' '; /* Month */ memcpy(buf, mk_date_ym[gtm->tm_mon], 4); buf += 4; /* Year */ *buf++ = ('0' + (year / 1000) % 10); *buf++ = ('0' + (year / 100) % 10); *buf++ = ('0' + (year / 10) % 10); *buf++ = ('0' + (year % 10)); *buf++ = ' '; /* Hour */ *buf++ = ('0' + (hour / 10)); *buf++ = ('0' + (hour % 10)); *buf++ = ':'; /* Minutes */ *buf++ = ('0' + (min / 10)); *buf++ = ('0' + (min % 10)); *buf++ = ':'; /* Seconds */ *buf++ = ('0' + (sec / 10)); *buf++ = ('0' + (sec % 10)); /* GMT Time zone + CRLF */ memcpy(buf, " GMT\r\n\0", 7); /* Add new entry to the cache */ mk_utils_gmt_cache_add(*data, date); /* Set mk_ptr_t data len */ return size; }
/* Send response headers */ int mk_header_send(int fd, struct client_session *cs, struct session_request *sr) { int i=0; unsigned long len = 0; char *buffer = 0; mk_pointer response; struct response_headers *sh; struct mk_iov *iov; sh = &sr->headers; iov = mk_header_iov_get(); /* HTTP Status Code */ if (sh->status == MK_CUSTOM_STATUS) { response.data = sh->custom_status.data; response.len = sh->custom_status.len; } else { for (i=0; i < status_response_len; i++) { if (status_response[i].status == sh->status) { response.data = status_response[i].response; response.len = status_response[i].length; break; } } } /* Invalid status set */ mk_bug(i == status_response_len); mk_header_iov_add_entry(iov, response, mk_iov_none, MK_IOV_NOT_FREE_BUF); /* Server details */ mk_iov_add_entry(iov, sr->host_conf->header_host_signature.data, sr->host_conf->header_host_signature.len, mk_iov_crlf, MK_IOV_NOT_FREE_BUF); /* Date */ mk_iov_add_entry(iov, mk_header_short_date.data, mk_header_short_date.len, header_current_time, MK_IOV_NOT_FREE_BUF); /* Last-Modified */ if (sh->last_modified > 0) { mk_pointer *lm; lm = mk_cache_get(mk_cache_header_lm); lm->len = mk_utils_utime2gmt(&lm->data, sh->last_modified); mk_iov_add_entry(iov, mk_header_last_modified.data, mk_header_last_modified.len, *lm, MK_IOV_NOT_FREE_BUF); } /* Connection */ if (sh->connection == 0) { if (mk_http_keepalive_check(cs) == 0) { if (sr->connection.len > 0) { /* Get cached mk_pointers */ mk_pointer *ka_format = mk_cache_get(mk_cache_header_ka); mk_pointer *ka_header = mk_cache_get(mk_cache_header_ka_max); /* Compose header and add entries to iov */ mk_string_itop(config->max_keep_alive_request - cs->counter_connections, ka_header); mk_iov_add_entry(iov, ka_format->data, ka_format->len, mk_iov_none, MK_IOV_NOT_FREE_BUF); mk_iov_add_entry(iov, ka_header->data, ka_header->len, mk_header_conn_ka, MK_IOV_NOT_FREE_BUF); } } else { mk_iov_add_entry(iov, mk_header_conn_close.data, mk_header_conn_close.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); } } /* Location */ if (sh->location != NULL) { mk_iov_add_entry(iov, mk_header_short_location.data, mk_header_short_location.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); mk_iov_add_entry(iov, sh->location, strlen(sh->location), mk_iov_crlf, MK_IOV_FREE_BUF); } /* allowed methods */ if (sh->allow_methods.len > 0) { mk_iov_add_entry(iov, mk_header_allow.data, mk_header_allow.len, sh->allow_methods, MK_IOV_NOT_FREE_BUF) ; } /* Content type */ if (sh->content_type.len > 0) { mk_iov_add_entry(iov, mk_header_short_ct.data, mk_header_short_ct.len, sh->content_type, MK_IOV_NOT_FREE_BUF); } /* * Transfer Encoding: the transfer encoding header is just sent when * the response has some content defined by the HTTP status response */ if ((sh->status < MK_REDIR_MULTIPLE) || (sh->status > MK_REDIR_USE_PROXY)) { switch (sh->transfer_encoding) { case MK_HEADER_TE_TYPE_CHUNKED: mk_iov_add_entry(iov, mk_header_te_chunked.data, mk_header_te_chunked.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); break; } } /* Content-Encoding */ if (sh->content_encoding.len > 0) { mk_iov_add_entry(iov, mk_header_content_encoding.data, mk_header_content_encoding.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); mk_iov_add_entry(iov, sh->content_encoding.data, sh->content_encoding.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); } /* Content-Length */ if (sh->content_length >= 0) { /* Map content length to MK_POINTER */ mk_pointer *cl; cl = mk_cache_get(mk_cache_header_cl); mk_string_itop(sh->content_length, cl); /* Set headers */ mk_iov_add_entry(iov, mk_header_content_length.data, mk_header_content_length.len, *cl, MK_IOV_NOT_FREE_BUF); } if ((sh->content_length != 0 && (sh->ranges[0] >= 0 || sh->ranges[1] >= 0)) && config->resume == MK_TRUE) { buffer = 0; /* yyy- */ if (sh->ranges[0] >= 0 && sh->ranges[1] == -1) { mk_string_build(&buffer, &len, "%s bytes %d-%ld/%ld", RH_CONTENT_RANGE, sh->ranges[0], (sh->real_length - 1), sh->real_length); mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF); } /* yyy-xxx */ if (sh->ranges[0] >= 0 && sh->ranges[1] >= 0) { mk_string_build(&buffer, &len, "%s bytes %d-%d/%ld", RH_CONTENT_RANGE, sh->ranges[0], sh->ranges[1], sh->real_length); mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF); } /* -xxx */ if (sh->ranges[0] == -1 && sh->ranges[1] > 0) { mk_string_build(&buffer, &len, "%s bytes %ld-%ld/%ld", RH_CONTENT_RANGE, (sh->real_length - sh->ranges[1]), (sh->real_length - 1), sh->real_length); mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF); } } mk_socket_set_cork_flag(fd, TCP_CORK_ON); if (sh->cgi == SH_NOCGI || sh->breakline == MK_HEADER_BREAKLINE) { if (!sr->headers._extra_rows) { mk_iov_add_entry(iov, mk_iov_crlf.data, mk_iov_crlf.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); } else { mk_iov_add_entry(sr->headers._extra_rows, mk_iov_crlf.data, mk_iov_crlf.len, mk_iov_none, MK_IOV_NOT_FREE_BUF); } } mk_socket_sendv(fd, iov); if (sr->headers._extra_rows) { mk_socket_sendv(fd, sr->headers._extra_rows); mk_iov_free(sr->headers._extra_rows); sr->headers._extra_rows = NULL; } mk_header_iov_free(iov); sh->sent = MK_TRUE; return 0; }
static struct mk_iov *mk_header_iov_get() { return mk_cache_get(mk_cache_iov_header); }
/* *This function given a unix time, set in a mk_pointer * the date in the RFC1123 format like: * * Wed, 23 Jun 2010 22:32:01 GMT * * it also adds a 'CRLF' at the end */ int mk_utils_utime2gmt(char **data, time_t date) { int size = 31; unsigned int year; char *buf=0; struct tm *gtm; if (date == 0) { if ((date = time(NULL)) == -1) { return -1; } } /* Convert unix time to struct tm */ gtm = mk_cache_get(mk_cache_utils_gmtime); /* If this function was invoked from a non-thread context it should exit */ mk_bug(!gtm); gtm = gmtime_r(&date, gtm); if (!gtm) { return -1; } /* struct tm -> tm_year counts number of years after 1900 */ year = gtm->tm_year + 1900; /* Compose template */ buf = *data; /* Week day */ *buf++ = mk_date_wd[gtm->tm_wday][0]; *buf++ = mk_date_wd[gtm->tm_wday][1]; *buf++ = mk_date_wd[gtm->tm_wday][2]; *buf++ = ','; *buf++ = ' '; /* Day of the month */ *buf++ = ('0' + (gtm->tm_mday / 10)); *buf++ = ('0' + (gtm->tm_mday % 10)); *buf++ = ' '; /* Year month */ *buf++ = mk_date_ym[gtm->tm_mon][0]; *buf++ = mk_date_ym[gtm->tm_mon][1]; *buf++ = mk_date_ym[gtm->tm_mon][2]; *buf++ = ' '; /* Year */ *buf++ = ('0' + (year / 1000) % 10); *buf++ = ('0' + (year / 100) % 10); *buf++ = ('0' + (year / 10) % 10); *buf++ = ('0' + (year % 10)); *buf++ = ' '; /* Hour */ *buf++ = ('0' + (gtm->tm_hour / 10)); *buf++ = ('0' + (gtm->tm_hour % 10)); *buf++ = ':'; /* Minutes */ *buf++ = ('0' + (gtm->tm_min / 10)); *buf++ = ('0' + (gtm->tm_min % 10)); *buf++ = ':'; /* Seconds */ *buf++ = ('0' + (gtm->tm_sec / 10)); *buf++ = ('0' + (gtm->tm_sec % 10)); *buf++ = ' '; /* GMT Time zone + CRLF */ *buf++ = 'G'; *buf++ = 'M'; *buf++ = 'T'; *buf++ = '\r'; *buf++ = '\n'; *buf++ = '\0'; /* Set mk_pointer data len */ return size; }