Commit c6dac3c6 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

https: revector time handling

Also robustify time test.
parent 8c490ccf
...@@ -605,27 +605,27 @@ static const char vlc_http_months[12][4] = { ...@@ -605,27 +605,27 @@ static const char vlc_http_months[12][4] = {
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
}; };
static int vlc_http_msg_add_time(struct vlc_http_msg *m, const char *hname, int vlc_http_msg_add_time(struct vlc_http_msg *m, const char *hname,
const struct tm *restrict tm) const time_t *t)
{ {
struct tm tm;
if (gmtime_r(t, &tm) == NULL)
return -1;
return vlc_http_msg_add_header(m, hname, return vlc_http_msg_add_header(m, hname,
"%s, %02d %s %04d %02d:%02d:%02d GMT", "%s, %02d %s %04d %02d:%02d:%02d GMT",
vlc_http_days[tm->tm_wday], tm->tm_mday, vlc_http_days[tm.tm_wday], tm.tm_mday,
vlc_http_months[tm->tm_mon], vlc_http_months[tm.tm_mon],
1900 + tm->tm_year, 1900 + tm.tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec); tm.tm_hour, tm.tm_min, tm.tm_sec);
} }
int vlc_http_msg_add_atime(struct vlc_http_msg *m) int vlc_http_msg_add_atime(struct vlc_http_msg *m)
{ {
struct tm tm;
time_t now; time_t now;
time(&now); time(&now);
if (gmtime_r(&now, &tm) == NULL) return vlc_http_msg_add_time(m, "Date", &now);
return -1;
return vlc_http_msg_add_time(m, "Date", &tm);
} }
static time_t vlc_http_mktime(const char *str) static time_t vlc_http_mktime(const char *str)
...@@ -658,16 +658,22 @@ error: ...@@ -658,16 +658,22 @@ error:
return -1; /* invalid month */ 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) time_t vlc_http_msg_get_atime(const struct vlc_http_msg *m)
{ {
const char *str = vlc_http_msg_get_header(m, "Date"); return vlc_http_msg_get_time(m, "Date");
return (str != NULL) ? vlc_http_mktime(str) : -1;
} }
time_t vlc_http_msg_get_mtime(const struct vlc_http_msg *m) 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 vlc_http_msg_get_time(m, "Last-Modified");
return (str != NULL) ? vlc_http_mktime(str) : -1;
} }
unsigned vlc_http_msg_get_retry_after(const struct vlc_http_msg *m) unsigned vlc_http_msg_get_retry_after(const struct vlc_http_msg *m)
......
...@@ -74,6 +74,24 @@ int vlc_http_msg_add_agent(struct vlc_http_msg *m, const char *str); ...@@ -74,6 +74,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); 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. * Adds a Date header.
*/ */
......
...@@ -210,6 +210,8 @@ int main(void) ...@@ -210,6 +210,8 @@ int main(void)
assert(vlc_http_msg_add_atime(m) == 0); assert(vlc_http_msg_add_atime(m) == 0);
time_t t = vlc_http_msg_get_atime(m); time_t t = vlc_http_msg_get_atime(m);
assert(t != (time_t)-1); 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"); vlc_http_msg_add_header(m, "Content-Length", "1234");
assert(vlc_http_msg_get_size(m) == 1234); assert(vlc_http_msg_get_size(m) == 1234);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment