vlc | branch: master | Rémi Denis-Courmont <[email protected]> | Wed Dec 16 22:21:25 2015 +0200| [c6dac3c621e67161155dc0cdb6f0e50a62a9bbd5] | committer: Rémi Denis-Courmont
https: revector time handling Also robustify time test. > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=c6dac3c621e67161155dc0cdb6f0e50a62a9bbd5 --- modules/access/http/message.c | 36 +++++++++++++++++++++--------------- modules/access/http/message.h | 18 ++++++++++++++++++ modules/access/http/message_test.c | 2 ++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/modules/access/http/message.c b/modules/access/http/message.c index 13cd053..e7f5853 100644 --- a/modules/access/http/message.c +++ b/modules/access/http/message.c @@ -605,27 +605,27 @@ static const char vlc_http_months[12][4] = { "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -static int vlc_http_msg_add_time(struct vlc_http_msg *m, const char *hname, - const struct tm *restrict tm) +int vlc_http_msg_add_time(struct vlc_http_msg *m, const char *hname, + const time_t *t) { + struct tm tm; + + if (gmtime_r(t, &tm) == NULL) + return -1; return vlc_http_msg_add_header(m, hname, "%s, %02d %s %04d %02d:%02d:%02d GMT", - vlc_http_days[tm->tm_wday], tm->tm_mday, - vlc_http_months[tm->tm_mon], - 1900 + tm->tm_year, - tm->tm_hour, tm->tm_min, tm->tm_sec); + vlc_http_days[tm.tm_wday], tm.tm_mday, + vlc_http_months[tm.tm_mon], + 1900 + tm.tm_year, + tm.tm_hour, tm.tm_min, tm.tm_sec); } int vlc_http_msg_add_atime(struct vlc_http_msg *m) { - struct tm tm; time_t now; time(&now); - if (gmtime_r(&now, &tm) == NULL) - return -1; - - return vlc_http_msg_add_time(m, "Date", &tm); + return vlc_http_msg_add_time(m, "Date", &now); } static time_t vlc_http_mktime(const char *str) @@ -658,16 +658,22 @@ error: return -1; /* invalid month */ } +time_t vlc_http_msg_get_time(const struct vlc_http_msg *m, const char *name) +{ + const char *str = vlc_http_msg_get_header(m, name); + if (str == NULL) + return -1; + return vlc_http_mktime(str); +} + time_t vlc_http_msg_get_atime(const struct vlc_http_msg *m) { - const char *str = vlc_http_msg_get_header(m, "Date"); - return (str != NULL) ? vlc_http_mktime(str) : -1; + return vlc_http_msg_get_time(m, "Date"); } time_t vlc_http_msg_get_mtime(const struct vlc_http_msg *m) { - const char *str = vlc_http_msg_get_header(m, "Last-Modified"); - return (str != NULL) ? vlc_http_mktime(str) : -1; + return vlc_http_msg_get_time(m, "Last-Modified"); } unsigned vlc_http_msg_get_retry_after(const struct vlc_http_msg *m) diff --git a/modules/access/http/message.h b/modules/access/http/message.h index e96ffa1..93be452 100644 --- a/modules/access/http/message.h +++ b/modules/access/http/message.h @@ -75,6 +75,24 @@ int vlc_http_msg_add_agent(struct vlc_http_msg *m, const char *str); const char *vlc_http_msg_get_agent(const struct vlc_http_msg *m); /** + * Parses a timestamp header. + * + * @param name header field name + * @return a timestamp value, or -1 on error. + */ +time_t vlc_http_msg_get_time(const struct vlc_http_msg *m, const char *name); + +/** + * Adds a timestamp header. + * + * @param name header field name + * @param t pointer to timestamp + * @return 0 on success, -1 on error (errno is set accordingly) + */ +int vlc_http_msg_add_time(struct vlc_http_msg *m, const char *name, + const time_t *t); + +/** * Adds a Date header. */ int vlc_http_msg_add_atime(struct vlc_http_msg *m); diff --git a/modules/access/http/message_test.c b/modules/access/http/message_test.c index 821fb72..9653807 100644 --- a/modules/access/http/message_test.c +++ b/modules/access/http/message_test.c @@ -210,6 +210,8 @@ int main(void) assert(vlc_http_msg_add_atime(m) == 0); time_t t = vlc_http_msg_get_atime(m); assert(t != (time_t)-1); + assert(vlc_http_msg_add_time(m, "Last-Modified", &t) == 0); + assert(t == vlc_http_msg_get_mtime(m)); vlc_http_msg_add_header(m, "Content-Length", "1234"); assert(vlc_http_msg_get_size(m) == 1234); _______________________________________________ vlc-commits mailing list [email protected] https://mailman.videolan.org/listinfo/vlc-commits
