[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] Add functions to format and parse RFC1123-style times, for ...
- To: or-cvs@freehaven.net
- Subject: [or-cvs] Add functions to format and parse RFC1123-style times, for ...
- From: nickm@seul.org (Nick Mathewson)
- Date: Tue, 3 Aug 2004 21:11:17 -0400 (EDT)
- Delivered-to: archiver@seul.org
- Delivered-to: or-cvs-outgoing@seul.org
- Delivered-to: or-cvs@seul.org
- Delivery-date: Tue, 03 Aug 2004 21:11:30 -0400
- Reply-to: or-dev@freehaven.net
- Sender: owner-or-cvs@freehaven.net
Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv425/src/common
Modified Files:
util.c util.h
Log Message:
Add functions to format and parse RFC1123-style times, for HTTP protocol.
Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.118
retrieving revision 1.119
diff -u -d -r1.118 -r1.119
--- util.c 3 Aug 2004 23:57:04 -0000 1.118
+++ util.c 4 Aug 2004 01:11:15 -0000 1.119
@@ -914,6 +914,57 @@
return ret;
}
+/* strftime is locale-specific, so we need to replace those parts */
+static const char *WEEKDAY_NAMES[] =
+ { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+static const char *MONTH_NAMES[] =
+ { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+void tor_format_rfc1123_time(char *buf, time_t t) {
+ struct tm *tm = gmtime(&t);
+
+ strftime(buf, RFC1123_TIME_LEN+1, "XXX, %d XXX %Y %H:%M:%S GMT", tm);
+ tor_assert(tm->tm_wday >= 0 && tm->tm_wday <= 6);
+ memcpy(buf, WEEKDAY_NAMES[tm->tm_wday], 3);
+ tor_assert(tm->tm_wday >= 0 && tm->tm_mon <= 11);
+ memcpy(buf+8, MONTH_NAMES[tm->tm_mon], 3);
+}
+
+int tor_parse_rfc1123_time(const char *buf, time_t *t) {
+ struct tm tm;
+ char month[4];
+ char weekday[4];
+ int i, m;
+
+ if (strlen(buf) != RFC1123_TIME_LEN)
+ return -1;
+ memset(&tm, 0, sizeof(tm));
+ if (sscanf(buf, "%3s, %d %3s %d %d:%d:%d GMT", weekday,
+ &tm.tm_mday, month, &tm.tm_year, &tm.tm_hour,
+ &tm.tm_min, &tm.tm_sec) < 7) {
+ log_fn(LOG_WARN, "Got invalid RFC1123 time \"%s\"", buf);
+ return -1;
+ }
+
+ m = -1;
+ for (i = 0; i < 12; ++i) {
+ if (!strcmp(month, MONTH_NAMES[i])) {
+ m = i;
+ break;
+ }
+ }
+ if (m<0) {
+ log_fn(LOG_WARN, "Got invalid RFC1123 time \"%s\"", buf);
+ return -1;
+ }
+
+ tm.tm_mon = m;
+ tm.tm_year -= 1900;
+ *t = tor_timegm(&tm);
+ return 0;
+}
+
/*
* Low-level I/O.
*/
Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.76
retrieving revision 1.77
diff -u -d -r1.76 -r1.77
--- util.h 3 Aug 2004 23:31:22 -0000 1.76
+++ util.h 4 Aug 2004 01:11:15 -0000 1.77
@@ -201,6 +201,9 @@
void tv_add(struct timeval *a, struct timeval *b);
int tv_cmp(struct timeval *a, struct timeval *b);
time_t tor_timegm(struct tm *tm);
+#define RFC1123_TIME_LEN 29
+void tor_format_rfc1123_time(char *buf, time_t t);
+int tor_parse_rfc1123_time(const char *buf, time_t *t);
int write_all(int fd, const char *buf, size_t count, int isSocket);
int read_all(int fd, char *buf, size_t count, int isSocket);