1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-06 05:02:40 +03:00

gmtime now thread safe

This commit is contained in:
Lammert Bies
2017-01-01 03:58:09 +01:00
parent 8af8bdc2c9
commit 7c57adf9a1
5 changed files with 37 additions and 86 deletions

View File

@@ -414,8 +414,7 @@ OBJLIST = \
${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} \
${OBJDIR}httplib_pthread_self${OBJEXT} \ ${OBJDIR}httplib_pthread_self${OBJEXT} \
${OBJDIR}httplib_pthread_setspecific${OBJEXT} \ ${OBJDIR}httplib_pthread_setspecific${OBJEXT} \
${OBJDIR}wince_gmtime${OBJEXT} \ ${OBJDIR}httplib_gmtime_r${OBJEXT} \
${OBJDIR}wince_gmtime_s${OBJEXT} \
${OBJDIR}httplib_localtime_r${OBJEXT} \ ${OBJDIR}httplib_localtime_r${OBJEXT} \
${OBJDIR}wince_rename${OBJEXT} \ ${OBJDIR}wince_rename${OBJEXT} \
${OBJDIR}wince_stat${OBJEXT} \ ${OBJDIR}wince_stat${OBJEXT} \
@@ -1482,11 +1481,7 @@ ${OBJDIR}httplib_pthread_setspecific${OBJEXT} : ${SRCDIR}httplib_pthread_sets
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h
${OBJDIR}wince_gmtime${OBJEXT} : ${SRCDIR}wince_gmtime.c \ ${OBJDIR}httplib_gmtime_r${OBJEXT} : ${SRCDIR}httplib_gmtime_r.c \
${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h
${OBJDIR}wince_gmtime_s${OBJEXT} : ${SRCDIR}wince_gmtime_s.c \
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${SRCDIR}httplib_utils.h \ ${SRCDIR}httplib_utils.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h

View File

@@ -917,6 +917,7 @@ LIBHTTP_API enum debug_level_t httplib_get_debug_level( struct httplib_context
LIBHTTP_API const char * httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen ); LIBHTTP_API const char * httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen );
LIBHTTP_API uint64_t httplib_get_random( void ); LIBHTTP_API uint64_t httplib_get_random( void );
LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn ); LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn );
LIBHTTP_API struct tm * httplib_gmtime_r( const time_t *clock, struct tm *result );
LIBHTTP_API int httplib_kill( pid_t pid, int sig_num ); LIBHTTP_API int httplib_kill( pid_t pid, int sig_num );
LIBHTTP_API struct tm * httplib_localtime_r( const time_t *clock, struct tm *result ); LIBHTTP_API struct tm * httplib_localtime_r( const time_t *clock, struct tm *result );
LIBHTTP_API int httplib_mkdir( const char *path, int mode ); LIBHTTP_API int httplib_mkdir( const char *path, int mode );

View File

@@ -33,13 +33,11 @@
* included in all responses other than 100, 101, 5xx. */ * included in all responses other than 100, 101, 5xx. */
void XX_httplib_gmt_time_string( char *buf, size_t buf_len, time_t *t ) { void XX_httplib_gmt_time_string( char *buf, size_t buf_len, time_t *t ) {
struct tm *tm; struct tm tmm;
if ( buf == NULL || buf_len < 1 ) return; if ( buf == NULL || buf_len < 1 ) return;
tm = ( t != NULL ) ? gmtime(t) : NULL; if ( httplib_gmtime_r( t, &tmm ) != NULL ) strftime( buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", &tmm );
if ( tm != NULL ) strftime( buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", tm );
else { else {
httplib_strlcpy( buf, "Thu, 01 Jan 1970 00:00:00 GMT", buf_len ); httplib_strlcpy( buf, "Thu, 01 Jan 1970 00:00:00 GMT", buf_len );

View File

@@ -28,46 +28,54 @@
#include "httplib_main.h" #include "httplib_main.h"
#include "httplib_utils.h" #include "httplib_utils.h"
#if defined(_WIN32_WCE)
/* /*
* struct tm *gmtime_s( const time_t *ptime, struct tm *ptm ); * struct tm *httplib_gmtime_r( const time_t *clock, struct tm *result );
* *
* The function gmtime_s() converts a number of seconds since the EPOCH to * The function httplib_gmtime_r() returns a converted time to tm structure.
* a tm time structure. This standard function is not available on all * No timezone conversion takes place. UTC as zone is assumed.
* platforms and this implementation provides the functionality for Windows CE.
*/ */
struct tm * gmtime_s( const time_t *ptime, struct tm *ptm ) { struct tm * httplib_gmtime_r( const time_t *clock, struct tm *result ) {
#if defined(_WIN32_CE)
int a; int a;
int doy; int doy;
FILETIME ft; FILETIME ft;
SYSTEMTIME st; SYSTEMTIME st;
if ( ptime == NULL || ptm == NULL ) return NULL; if ( clock == NULL || result == NULL ) return NULL;
*(int64_t)&ft = ((int64_t)*ptime) * RATE_DIFF * EPOCH_DIFF; *(int64_t)&ft = ((int64_t)*clock) * RATE_DIFF * EPOCH_DIFF;
FileTimeToSystemTime( & ft, & st ); FileTimeToSystemTime( & ft, & st );
ptm->tm_year = st.wYear - 1900; result->tm_year = st.wYear - 1900;
ptm->tm_mon = st.wMonth - 1; result->tm_mon = st.wMonth - 1;
ptm->tm_wday = st.wDayOfWeek; result->tm_wday = st.wDayOfWeek;
ptm->tm_mday = st.wDay; result->tm_mday = st.wDay;
ptm->tm_hour = st.wHour; result->tm_hour = st.wHour;
ptm->tm_min = st.wMinute; result->tm_min = st.wMinute;
ptm->tm_sec = st.wSecond; result->tm_sec = st.wSecond;
ptm->tm_isdst = false; result->tm_isdst = false;
doy = ptm->tm_mday; doy = result->tm_mday;
for (a=0; a<ptm->tm_mon; a++) doy += days_per_month[a]; for (a=0; a<result->tm_mon; a++) doy += days_per_month[a];
if ( ptm->tm_mon >= 2 && LEAP_YEAR( ptm->tm_year+1900 ) ) doy++; if ( result->tm_mon >= 2 && LEAP_YEAR( result->tm_year+1900 ) ) doy++;
ptm->tm_yday = doy; result->tm_yday = doy;
return ptm; return result;
} /* gmtime_s */ #elif defined(_WIN32)
#endif /* defined(_WIN32_WCE) */ if ( gmtime_s( result, clock ) == 0 ) return result;
return NULL;
#else
return gmtime_r( clock, result );
#endif
} /* httplib_gmtime_r */

View File

@@ -1,51 +0,0 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2016 the Civetweb developers
* Copyright (c) 2004-2013 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ============
* Release: 2.0
*/
#include "httplib_main.h"
#if defined(_WIN32_WCE)
/*
* struct tm *gmtime( const time_t *ptime );
*
* The function gmtime() is a system function which converts the number since
* EPOCH to a time structure based on UTC. This function is not available on
* all platforms and the implementation here can be used on Windows CE. Please
* note that this implementation is nut fully thread safe if the function is
* also called from threads which have been started outside of LibHTTP.
*/
struct tm *gmtime( const time_t *ptime ) {
int i;
i = XX_httplib_atomic_inc(&XX_httplib_tm_index) % MAX_WORKER_THREADS;
return gmtime_s( ptime, XX_httplib_tm_array + i );
} /* gmtime */
#endif /* defined(_WIN32_WCE) */