Last active
August 12, 2026 03:38
-
-
Save patrickgwsmith/8caaf4f2d07ef97a662486f791e4a07c to your computer and use it in GitHub Desktop.
allweeks.exe.xyz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <assert.h> | |
| #include <errno.h> | |
| #include <inttypes.h> | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <time.h> | |
| #include <unistd.h> | |
| #include <arpa/inet.h> | |
| #include <sys/socket.h> | |
| #include <sys/time.h> | |
| #define PASS_STR(str) (str), (sizeof(str) - 1) | |
| #define REQUEST_CAP 8192 | |
| #define OUTPUT_CAP (512 * 1024) | |
| #define MIN_YEAR 1583 | |
| #define MAX_YEAR 9999 | |
| [[noreturn]] void | |
| gameover(const char *msg, size_t msg_size) | |
| { | |
| fwrite(msg, 1, msg_size, stderr); | |
| fputs("\n", stderr); | |
| exit(-1); | |
| } | |
| #define GAME_OVER(str) gameover(PASS_STR(str)) | |
| static int | |
| request_headers_complete(const char *request, size_t request_size) | |
| { | |
| if (request_size < 4) return 0; | |
| for (size_t i = 3; i < request_size; i++) { | |
| if (request[i - 3] == '\r' | |
| && request[i - 2] == '\n' | |
| && request[i - 1] == '\r' | |
| && request[i] == '\n') { | |
| return 1; | |
| } | |
| } | |
| return 0; | |
| } | |
| static ssize_t | |
| read_request_headers(int client_fd, char *request, size_t capacity) | |
| { | |
| size_t request_size = 0; | |
| while (request_size < capacity - 1) { | |
| ssize_t read_size = recv( | |
| client_fd, | |
| request + request_size, | |
| capacity - request_size - 1, | |
| 0 | |
| ); | |
| if (read_size < 0) { | |
| if (errno == EINTR) continue; | |
| return -1; | |
| } | |
| if (read_size == 0) return (ssize_t)request_size; | |
| request_size += (size_t)read_size; | |
| request[request_size] = '\0'; | |
| if (request_headers_complete(request, request_size)) { | |
| return (ssize_t)request_size; | |
| } | |
| } | |
| errno = EMSGSIZE; | |
| return -1; | |
| } | |
| static int | |
| write_all(int fd, const char *bytes, size_t size) | |
| { | |
| size_t written = 0; | |
| while (written < size) { | |
| ssize_t result = write(fd, bytes + written, size - written); | |
| if (result < 0) { | |
| if (errno == EINTR) continue; | |
| return 0; | |
| } | |
| if (result == 0) return 0; | |
| written += (size_t)result; | |
| } | |
| return 1; | |
| } | |
| struct Arena { char *ptr; size_t remaining; }; | |
| static void | |
| arena_append(struct Arena *a, const char *bytes, size_t size) | |
| { | |
| assert(a->remaining >= size); | |
| memcpy(a->ptr, bytes, size); | |
| a->ptr += size; | |
| a->remaining -= size; | |
| } | |
| #define ARENA_APPEND(a, str) arena_append((a), PASS_STR(str)) | |
| ssize_t | |
| write_200_ok(int client_fd) | |
| { | |
| return write(client_fd, PASS_STR("HTTP/1.1 200 OK\r\n")); | |
| } | |
| ssize_t | |
| write_307_temporary_redirect(int client_fd) | |
| { | |
| return write(client_fd, PASS_STR("HTTP/1.1 307 Temporary Redirect\r\n")); | |
| } | |
| ssize_t | |
| write_400_bad_request(int client_fd) | |
| { | |
| return write(client_fd, PASS_STR("HTTP/1.1 400 Bad Request\r\n")); | |
| } | |
| ssize_t | |
| write_404_not_found(int client_fd) | |
| { | |
| return write(client_fd, PASS_STR("HTTP/1.1 404 Not Found\r\n")); | |
| } | |
| ssize_t | |
| write_405_method_not_allowed(int client_fd) | |
| { | |
| return write(client_fd, PASS_STR("HTTP/1.1 405 Method Not Allowed\r\n")); | |
| } | |
| static void | |
| arena_u32_format( | |
| struct Arena *a, | |
| uint32_t value, | |
| const char *format, | |
| size_t max_size | |
| ) { | |
| // Add nul-terminator | |
| max_size += 1; | |
| assert(a->remaining >= max_size); | |
| char *p = a->ptr; | |
| int n = snprintf(p, max_size, format, value); | |
| assert(n >= 0 && (size_t)n < max_size); | |
| a->ptr += n; | |
| a->remaining -= n; | |
| } | |
| static void | |
| arena_u32_decimal(struct Arena *a, uint32_t value) | |
| { | |
| arena_u32_format(a, value, "%" PRIu32, 10); | |
| } | |
| static void | |
| arena_u32_hex_6(struct Arena *a, uint32_t value) | |
| { | |
| arena_u32_format(a, value, "%06" PRIx32, 6); | |
| } | |
| static void | |
| arena_u32_2_digit(struct Arena *a, uint32_t value) | |
| { | |
| arena_u32_format(a, value, "%02" PRIu32, 2); | |
| } | |
| static void | |
| arena_u32_4_digit(struct Arena *a, uint32_t value) | |
| { | |
| arena_u32_format(a, value, "%04" PRIu32, 4); | |
| } | |
| enum Page { | |
| PAGE_NOT_FOUND, | |
| PAGE_CURRENT_YEAR_REDIRECT, | |
| PAGE_DECADE_REDIRECT, | |
| PAGE_BRAND_REDIRECT, | |
| PAGE_COLOR_YEAR, | |
| PAGE_COLOR_DECADE, | |
| }; | |
| enum CalendarDirection { | |
| CALENDAR_DIRECTION_NONE, | |
| CALENDAR_DIRECTION_PREVIOUS, | |
| CALENDAR_DIRECTION_NEXT, | |
| }; | |
| static enum CalendarDirection | |
| year_direction(uint32_t year, uint32_t previous_year) | |
| { | |
| if (year > previous_year) return CALENDAR_DIRECTION_NEXT; | |
| if (year < previous_year) return CALENDAR_DIRECTION_PREVIOUS; | |
| return CALENDAR_DIRECTION_NONE; | |
| } | |
| struct PageInput { | |
| enum Page kind; | |
| uint32_t random_seed; | |
| uint32_t hex; | |
| uint32_t year; | |
| uint8_t month; | |
| uint8_t day; | |
| enum CalendarDirection calendar_direction; | |
| }; | |
| struct QueryInput { | |
| uint32_t hex; | |
| uint32_t previous_year; | |
| int has_hex; | |
| int has_previous_year; | |
| }; | |
| enum RouteKind { | |
| ROUTE_NOT_FOUND, | |
| ROUTE_ROOT, | |
| ROUTE_ROBOTS, | |
| ROUTE_YEAR, | |
| ROUTE_DECADE, | |
| ROUTE_BRAND_REDIRECT, | |
| }; | |
| struct Route { | |
| enum RouteKind kind; | |
| struct PageInput page; | |
| }; | |
| static uint32_t | |
| random_u32(uint32_t seed) | |
| { | |
| seed ^= seed << 13; | |
| seed ^= seed >> 17; | |
| seed ^= seed << 5; | |
| return seed; | |
| } | |
| static uint32_t | |
| random_apple_color(uint32_t seed) | |
| { | |
| static const uint32_t colors[] = { | |
| 0x61bb46, | |
| 0xfdb827, | |
| 0xf5821f, | |
| 0xe03a3e, | |
| 0x963d97, | |
| 0x009ddc, | |
| }; | |
| return colors[seed % (sizeof(colors) / sizeof(colors[0]))]; | |
| } | |
| static uint32_t | |
| random_atp_color(uint32_t seed) | |
| { | |
| static const uint32_t colors[] = { | |
| 0x61bb46, | |
| 0xfdb827, | |
| 0xf5821f, | |
| 0xe03a3e, | |
| 0x963d97, | |
| 0x009ddc, | |
| 0x000000, | |
| }; | |
| return colors[seed % (sizeof(colors) / sizeof(colors[0]))]; | |
| } | |
| static uint32_t | |
| random_google_color(uint32_t seed) | |
| { | |
| static const uint32_t colors[] = { | |
| 0x4285f4, | |
| 0xea4335, | |
| 0xfbbc05, | |
| 0x34a853, | |
| }; | |
| return colors[seed % (sizeof(colors) / sizeof(colors[0]))]; | |
| } | |
| static uint32_t | |
| random_ebay_color(uint32_t seed) | |
| { | |
| static const uint32_t colors[] = { | |
| 0xe53238, | |
| 0x0064d2, | |
| 0xf5af02, | |
| 0x86b817, | |
| }; | |
| return colors[seed % (sizeof(colors) / sizeof(colors[0]))]; | |
| } | |
| static uint32_t | |
| random_python_color(uint32_t seed) | |
| { | |
| static const uint32_t colors[] = { | |
| 0x3776ab, | |
| 0xffd343, | |
| }; | |
| return colors[seed % (sizeof(colors) / sizeof(colors[0]))]; | |
| } | |
| static int | |
| path_equals(const char *path, size_t path_size, const char *expected, size_t expected_size) | |
| { | |
| return path_size == expected_size && memcmp(path, expected, expected_size) == 0; | |
| } | |
| static uint8_t | |
| hue_to_rgb(uint16_t low, uint16_t high, int16_t hue) | |
| { | |
| if (hue < 0) hue += 255; | |
| if (hue > 255) hue -= 255; | |
| if (hue < 43) { | |
| return (uint8_t)(low + (high - low) * 6 * hue / 255); | |
| } | |
| if (hue < 128) return (uint8_t)high; | |
| if (hue < 170) { | |
| return (uint8_t)(low + (high - low) * 6 * (170 - hue) / 255); | |
| } | |
| return (uint8_t)low; | |
| } | |
| static uint32_t | |
| random_hsl_color(uint32_t seed) | |
| { | |
| seed = random_u32(seed); | |
| uint8_t hue = (uint8_t)seed; | |
| seed = random_u32(seed); | |
| uint8_t saturation_percent = (uint8_t)(40 + seed % 41); | |
| seed = random_u32(seed); | |
| uint8_t lightness_percent = (uint8_t)(seed % 101); | |
| uint16_t saturation = (uint16_t)(saturation_percent * 255 / 100); | |
| uint16_t lightness = (uint16_t)(lightness_percent * 255 / 100); | |
| uint16_t high = lightness < 128 | |
| ? (uint16_t)(lightness * (255 + saturation) / 255) | |
| : (uint16_t)(lightness + saturation - lightness * saturation / 255); | |
| uint16_t low = (uint16_t)(2 * lightness - high); | |
| uint8_t red = hue_to_rgb(low, high, (int16_t)(hue + 85)); | |
| uint8_t green = hue_to_rgb(low, high, hue); | |
| uint8_t blue = hue_to_rgb(low, high, (int16_t)(hue - 85)); | |
| return ((uint32_t)red << 16) | ((uint32_t)green << 8) | blue; | |
| } | |
| static uint32_t | |
| initial_random_seed(void) | |
| { | |
| struct timespec now; | |
| if (timespec_get(&now, TIME_UTC) != TIME_UTC) { | |
| GAME_OVER("Cannot read the current time."); | |
| } | |
| uint64_t time = (uint64_t)now.tv_sec ^ ((uint64_t)now.tv_nsec << 32); | |
| uint32_t seed = (uint32_t)(time ^ (time >> 32) ^ (uint64_t)getpid()); | |
| return seed == 0 ? UINT32_C(0x9e3779b9) : seed; | |
| } | |
| static uint32_t | |
| local_year_now(void) | |
| { | |
| time_t now = time(NULL); | |
| if (now == (time_t)-1) GAME_OVER("Cannot read the current time."); | |
| struct tm *local = localtime(&now); | |
| if (local == NULL) GAME_OVER("Cannot convert the current time."); | |
| uint32_t year = (uint32_t)(local->tm_year + 1900); | |
| if (year < MIN_YEAR || year > MAX_YEAR) { | |
| GAME_OVER("Current year is outside the supported range."); | |
| } | |
| return year; | |
| } | |
| static int | |
| parse_hex_6(const char *path, size_t path_size, uint32_t *value) | |
| { | |
| if (path_size != 6) return 0; | |
| uint32_t result = 0; | |
| for (size_t i = 0; i < path_size; i++) { | |
| char c = path[i]; | |
| int is_numeric = c >= '0' && c <= '9'; | |
| int is_hex = c >= 'a' && c <= 'f'; | |
| if (!is_numeric && !is_hex) return 0; | |
| result <<= 4; | |
| result += is_numeric ? (uint32_t)(c - '0') : 0; | |
| result += is_hex ? (uint32_t)(c - 'a' + 10) : 0; | |
| } | |
| *value = result; | |
| return 1; | |
| } | |
| static int | |
| parse_year_4(const char *path, size_t path_size, uint32_t *result_year) | |
| { | |
| if (path_size != 4) return 0; | |
| uint32_t year = 0; | |
| for (size_t i = 0; i < path_size; i++) { | |
| if (path[i] < '0' || path[i] > '9') return 0; | |
| year = year * 10 + (uint32_t)(path[i] - '0'); | |
| } | |
| if (year < MIN_YEAR || year > MAX_YEAR) return 0; | |
| *result_year = year; | |
| return 1; | |
| } | |
| static int | |
| parse_decade(const char *path, size_t path_size, uint32_t *result_year) | |
| { | |
| if (path_size != 5 || path[4] != 's') return 0; | |
| uint32_t first_year = 0; | |
| for (size_t i = 0; i < 4; i++) { | |
| if (path[i] < '0' || path[i] > '9') return 0; | |
| first_year = first_year * 10 + (uint32_t)(path[i] - '0'); | |
| } | |
| if (first_year % 10 != 0) return 0; | |
| uint32_t last_year = first_year + 9; | |
| if (last_year < MIN_YEAR || first_year > MAX_YEAR) return 0; | |
| *result_year = first_year; | |
| return 1; | |
| } | |
| static int | |
| parse_query(const char *query, size_t query_size, struct QueryInput *result) | |
| { | |
| while (query_size > 0) { | |
| const char *separator = memchr(query, '&', query_size); | |
| size_t parameter_size = separator | |
| ? (size_t)(separator - query) | |
| : query_size; | |
| if (parameter_size == sizeof("c=000000") - 1 | |
| && memcmp(query, "c=", sizeof("c=") - 1) == 0 | |
| && !result->has_hex | |
| && parse_hex_6( | |
| query + sizeof("c=") - 1, | |
| 6, | |
| &result->hex | |
| )) { | |
| result->has_hex = 1; | |
| } else if (parameter_size == sizeof("was=0000") - 1 | |
| && memcmp(query, "was=", sizeof("was=") - 1) == 0 | |
| && !result->has_previous_year | |
| && parse_year_4( | |
| query + sizeof("was=") - 1, | |
| 4, | |
| &result->previous_year | |
| )) { | |
| result->has_previous_year = 1; | |
| } else if ((parameter_size >= sizeof("c=") - 1 | |
| && memcmp(query, "c=", sizeof("c=") - 1) == 0) | |
| || (parameter_size >= sizeof("was=") - 1 | |
| && memcmp(query, "was=", sizeof("was=") - 1) == 0)) { | |
| return 0; | |
| } | |
| if (!separator) break; | |
| query_size -= parameter_size + 1; | |
| query = separator + 1; | |
| if (query_size == 0) return 0; | |
| } | |
| return 1; | |
| } | |
| static int | |
| is_leap_year(uint32_t year) | |
| { | |
| if (year % 400 == 0) return 1; | |
| if (year % 100 == 0) return 0; | |
| return year % 4 == 0; | |
| } | |
| static uint8_t | |
| days_in_month(uint32_t year, uint8_t month) | |
| { | |
| static const uint8_t days[] = { | |
| 31, 28, 31, 30, 31, 30, | |
| 31, 31, 30, 31, 30, 31, | |
| }; | |
| assert(month >= 1 && month <= 12); | |
| if (month == 2 && is_leap_year(year)) return 29; | |
| return days[month - 1]; | |
| } | |
| static const char *MONTH_NAMES[] = { | |
| "January", "February", "March", "April", "May", "June", | |
| "July", "August", "September", "October", "November", "December", | |
| }; | |
| static void | |
| date_from_day_of_year( | |
| uint32_t year, | |
| uint16_t day_of_year, | |
| uint8_t *result_month, | |
| uint8_t *result_day | |
| ) | |
| { | |
| uint8_t month = 1; | |
| while (day_of_year >= days_in_month(year, month)) { | |
| day_of_year = (uint16_t)(day_of_year - days_in_month(year, month)); | |
| month++; | |
| } | |
| *result_month = month; | |
| *result_day = (uint8_t)(day_of_year + 1); | |
| } | |
| // Returns 0 for Sunday through 6 for Saturday. | |
| static uint8_t | |
| day_of_week_gregorian(uint32_t year, uint8_t month, uint8_t day) | |
| { | |
| static const int offsets[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; | |
| int y = (int)year; | |
| if (month < 3) y--; | |
| return (uint8_t)((y + y / 4 - y / 100 + y / 400 + offsets[month - 1] + day) % 7); | |
| } | |
| static uint16_t | |
| day_of_year(uint32_t year, uint8_t month, uint8_t day) | |
| { | |
| uint16_t result = day; | |
| for (uint8_t previous_month = 1; previous_month < month; previous_month++) { | |
| result = (uint16_t)(result + days_in_month(year, previous_month)); | |
| } | |
| return result; | |
| } | |
| static uint8_t | |
| iso_weeks_in_year(uint32_t year) | |
| { | |
| uint8_t january_first = day_of_week_gregorian(year, 1, 1); | |
| return (uint8_t)(january_first == 4 | |
| || (january_first == 3 && is_leap_year(year)) | |
| ? 53 | |
| : 52); | |
| } | |
| static uint8_t | |
| iso_week_number(uint32_t year, uint8_t month, uint8_t day) | |
| { | |
| uint16_t ordinal = day_of_year(year, month, day); | |
| uint8_t sunday_first = day_of_week_gregorian(year, month, day); | |
| uint8_t monday_first = (uint8_t)((sunday_first + 6) % 7 + 1); | |
| int week = ((int)ordinal - monday_first + 10) / 7; | |
| if (week < 1) return iso_weeks_in_year(year - 1); | |
| if (week > iso_weeks_in_year(year)) return 1; | |
| return (uint8_t)week; | |
| } | |
| static void | |
| render_randomizer(struct Arena *output, struct PageInput input) | |
| { | |
| ARENA_APPEND(output, | |
| "<form class=randomizer action='"); | |
| if (input.kind == PAGE_COLOR_YEAR) { | |
| ARENA_APPEND(output, "/"); | |
| arena_u32_4_digit(output, input.year); | |
| } else if (input.kind == PAGE_COLOR_DECADE) { | |
| ARENA_APPEND(output, "/"); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, "s"); | |
| } else { | |
| ARENA_APPEND(output, "/"); | |
| } | |
| ARENA_APPEND(output, | |
| "'><input type=hidden name=c value="); | |
| arena_u32_hex_6(output, random_hsl_color(input.random_seed)); | |
| ARENA_APPEND(output, | |
| "><button><span aria-hidden=true>🪄</span>" | |
| "<span class=visually-hidden>Randomize color</span></button></form>\n"); | |
| } | |
| static void | |
| render_not_found(struct Arena *output, struct PageInput input) | |
| { | |
| ARENA_APPEND(output, "<title>Page not found</title>\n"); | |
| render_randomizer(output, input); | |
| ARENA_APPEND(output, | |
| "<p>Page not found</p>\n"); | |
| } | |
| static void | |
| render_page_color(struct Arena *output, uint32_t hex) | |
| { | |
| ARENA_APPEND(output, | |
| "<style>\n" | |
| ":root {\n" | |
| "--bg: #"); | |
| arena_u32_hex_6(output, hex); | |
| ARENA_APPEND(output, | |
| ";}\n" | |
| "::view-transition-group(root) { animation-duration: 1s; }\n" | |
| "</style>\n"); | |
| } | |
| static void | |
| render_year_url(struct Arena *output, uint32_t hex, uint32_t year) | |
| { | |
| ARENA_APPEND(output, "/"); | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "?c="); | |
| arena_u32_hex_6(output, hex); | |
| } | |
| static void | |
| render_calendar_style( | |
| struct Arena *output, | |
| struct PageInput input | |
| ) | |
| { | |
| ARENA_APPEND(output, | |
| "<style>\n" | |
| ":root { --calendar-border-width: .5px; --calendar-border-color: color-mix(in srgb, currentColor 25%, transparent); }\n" | |
| "main { max-width: 44rem; margin: 1rlh auto 2rlh; padding: 0 1rem; }\n" | |
| "h1 { margin: 0; }\n" | |
| ".year-nav { display: flex; align-items: center; justify-content: space-between; margin: 0 0 1rlh; }\n" | |
| "table { width: 100%; border: var(--calendar-border-width) solid var(--calendar-border-color); border-spacing: 0; background: var(--bg); color: inherit; }\n" | |
| "th, td { padding: .5rlh .75rem; border-right: var(--calendar-border-width) solid var(--calendar-border-color); border-bottom: var(--calendar-border-width) solid var(--calendar-border-color); text-align: center; }\n" | |
| "th { color: inherit; font-weight: 500; }\n" | |
| "thead th { position: sticky; top: 0; z-index: 2; background: var(--bg); box-shadow: inset 0 calc(-1 * var(--calendar-border-width)) var(--calendar-border-color); }\n" | |
| "th:last-child, td:last-child { border-right: 0; } tr:last-child th, tr:last-child td { border-bottom: 0; }\n" | |
| "td:empty { background: transparent; }\n" | |
| ".year .day-column { width: 11%; } .year .month-column { width: 16%; } .year .week-column { width: 7%; }\n" | |
| ".year tbody th { text-align: right; } .year th.month { padding: 0; vertical-align: top; } .year th.week { padding: 0; opacity: .7; }\n" | |
| ".year th.month a { position: sticky; top: 2rlh; display: block; padding: .5rlh .75rem; background: transparent; text-decoration: none; }\n" | |
| ".year td[id], .year a[id], .decade-calendar h2[id] { scroll-margin-top: 2rlh; } .year td[id] { padding: 0; } .year td a, .year th.week a, .year th.week span { display: block; box-sizing: border-box; width: 100%; padding: .5rlh .75rem; text-decoration: none; }\n" | |
| ".odd-month-day, th.odd-month { background: color-mix(in srgb, var(--bg) 94%, black); }\n" | |
| ".year td:target, .year a:target { background: contrast-color(var(--bg)); color: var(--bg); opacity: 1; }\n" | |
| ".year th.month a:target { background: transparent; color: inherit; text-decoration: underline; }\n" | |
| ".decade-calendar { margin-top: 3rlh; } .decade-calendar h2 { margin: 0 0 1rlh; }\n" | |
| "@keyframes calendar-out-up { to { translate: 0 -125%; } }\n" | |
| "@keyframes calendar-in-bottom { from { translate: 0 125%; } }\n" | |
| "@keyframes calendar-out-down { to { translate: 0 125%; } }\n" | |
| "@keyframes calendar-in-top { from { translate: 0 -125%; } }\n"); | |
| if (input.calendar_direction != CALENDAR_DIRECTION_NONE) { | |
| ARENA_APPEND(output, "main { view-transition-name: calendar; }\n"); | |
| } | |
| if (input.kind == PAGE_COLOR_YEAR | |
| && input.calendar_direction == CALENDAR_DIRECTION_PREVIOUS) { | |
| ARENA_APPEND(output, | |
| "::view-transition-old(calendar) { animation: var(--page-transition-duration) ease-in-out both calendar-out-down; }\n" | |
| "::view-transition-new(calendar) { animation: var(--page-transition-duration) ease-in-out both calendar-in-top; }\n"); | |
| } else if (input.kind == PAGE_COLOR_YEAR | |
| && input.calendar_direction == CALENDAR_DIRECTION_NEXT) { | |
| ARENA_APPEND(output, | |
| "::view-transition-old(calendar) { animation: var(--page-transition-duration) ease-in-out both calendar-out-up; }\n" | |
| "::view-transition-new(calendar) { animation: var(--page-transition-duration) ease-in-out both calendar-in-bottom; }\n"); | |
| } | |
| ARENA_APPEND(output, "</style>\n"); | |
| } | |
| static void | |
| render_year_link( | |
| struct Arena *output, | |
| struct PageInput input, | |
| uint32_t year | |
| ) | |
| { | |
| ARENA_APPEND(output, "<a class=year-link href="); | |
| render_year_url(output, input.hex, year); | |
| ARENA_APPEND(output, "&was="); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, ">"); | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "</a>"); | |
| } | |
| static void | |
| render_date_fragment( | |
| struct Arena *output, | |
| uint32_t year, | |
| uint8_t month, | |
| uint8_t day | |
| ) | |
| { | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "-"); | |
| arena_u32_2_digit(output, month); | |
| ARENA_APPEND(output, "-"); | |
| arena_u32_2_digit(output, day); | |
| } | |
| static void | |
| render_month_fragment(struct Arena *output, uint32_t year, uint8_t month) | |
| { | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "-"); | |
| arena_u32_2_digit(output, month); | |
| } | |
| static void | |
| render_week_fragment(struct Arena *output, uint32_t year, uint8_t week) | |
| { | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "-W"); | |
| arena_u32_2_digit(output, week); | |
| } | |
| static void | |
| render_year_day( | |
| struct Arena *output, | |
| uint32_t year, | |
| uint8_t month, | |
| uint8_t day | |
| ) | |
| { | |
| ARENA_APPEND(output, "<td"); | |
| if (month % 2 == 1) ARENA_APPEND(output, " class=odd-month-day"); | |
| ARENA_APPEND(output, " id="); | |
| render_date_fragment(output, year, month, day); | |
| ARENA_APPEND(output, "><a href=#"); | |
| render_date_fragment(output, year, month, day); | |
| ARENA_APPEND(output, ">"); | |
| arena_u32_decimal(output, day); | |
| ARENA_APPEND(output, "</a></td>"); | |
| } | |
| static void | |
| render_calendar(struct Arena *output, struct PageInput input, int unique_iso_weeks) | |
| { | |
| ARENA_APPEND(output, | |
| "<table class=year>\n" | |
| "<colgroup><col class=day-column span=7><col class=month-column><col class=week-column></colgroup>\n" | |
| "<thead><tr><th>Mon</th><th>Tue</th><th>Wed</th>" | |
| "<th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th><th colspan=2><b>"); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, | |
| "</b></th></tr></thead>\n" | |
| "<tbody>\n"); | |
| uint8_t sunday_first = day_of_week_gregorian(input.year, 1, 1); | |
| uint8_t offset = (uint8_t)((sunday_first + 6) % 7); | |
| uint16_t days = (uint16_t)(is_leap_year(input.year) ? 366 : 365); | |
| uint16_t weeks = (uint16_t)((offset + days + 6) / 7); | |
| for (uint16_t week = 0; week < weeks; week++) { | |
| uint8_t row_month = 0; | |
| uint8_t row_iso_week = 0; | |
| uint32_t row_iso_year = input.year; | |
| for (uint8_t column = 0; column < 7; column++) { | |
| int32_t ordinal = (int32_t)(week * 7 + column) - offset; | |
| if (ordinal < 0 || ordinal >= days) continue; | |
| uint8_t month; | |
| uint8_t day; | |
| date_from_day_of_year( | |
| input.year, | |
| (uint16_t)ordinal, | |
| &month, | |
| &day | |
| ); | |
| if (row_iso_week == 0) { | |
| row_iso_week = iso_week_number(input.year, month, day); | |
| if (month == 1 && row_iso_week >= 52) { | |
| row_iso_year--; | |
| } else if (month == 12 && row_iso_week == 1) { | |
| row_iso_year++; | |
| } | |
| } | |
| if (day == 1) row_month = month; | |
| } | |
| ARENA_APPEND(output, "<tr>"); | |
| for (uint8_t column = 0; column < 7; column++) { | |
| int32_t ordinal = (int32_t)(week * 7 + column) - offset; | |
| if (ordinal < 0 || ordinal >= days) { | |
| ARENA_APPEND(output, "<td></td>"); | |
| continue; | |
| } | |
| uint8_t month; | |
| uint8_t day; | |
| date_from_day_of_year( | |
| input.year, | |
| (uint16_t)ordinal, | |
| &month, | |
| &day | |
| ); | |
| render_year_day(output, input.year, month, day); | |
| } | |
| if (row_month != 0) { | |
| uint16_t next_month_week = weeks; | |
| if (row_month < 12) { | |
| uint16_t next_month_ordinal = (uint16_t)( | |
| day_of_year(input.year, (uint8_t)(row_month + 1), 1) - 1 | |
| ); | |
| next_month_week = (uint16_t)((offset + next_month_ordinal) / 7); | |
| } | |
| uint16_t month_weeks = (uint16_t)(next_month_week - week); | |
| if (row_month % 2 == 1) { | |
| ARENA_APPEND(output, "<th class='month odd-month' rowspan="); | |
| } else { | |
| ARENA_APPEND(output, "<th class=month rowspan="); | |
| } | |
| arena_u32_decimal(output, month_weeks); | |
| ARENA_APPEND(output, "><a id="); | |
| render_month_fragment(output, input.year, row_month); | |
| ARENA_APPEND(output, " href=#"); | |
| render_month_fragment(output, input.year, row_month); | |
| ARENA_APPEND(output, ">"); | |
| const char *month_name = MONTH_NAMES[row_month - 1]; | |
| arena_append(output, month_name, strlen(month_name)); | |
| ARENA_APPEND(output, "</a></th>"); | |
| } | |
| ARENA_APPEND(output, "<th class=week scope=row>"); | |
| if (!unique_iso_weeks || row_iso_year == input.year) { | |
| ARENA_APPEND(output, "<a id="); | |
| render_week_fragment(output, row_iso_year, row_iso_week); | |
| ARENA_APPEND(output, " href=#"); | |
| render_week_fragment(output, row_iso_year, row_iso_week); | |
| ARENA_APPEND(output, ">"); | |
| } else { | |
| ARENA_APPEND(output, "<span>"); | |
| } | |
| ARENA_APPEND(output, "<i>W"); | |
| arena_u32_2_digit(output, row_iso_week); | |
| if (!unique_iso_weeks || row_iso_year == input.year) { | |
| ARENA_APPEND(output, "</i></a></th>"); | |
| } else { | |
| ARENA_APPEND(output, "</i></span></th>"); | |
| } | |
| ARENA_APPEND(output, "</tr>\n"); | |
| } | |
| ARENA_APPEND(output, "</tbody></table>\n"); | |
| } | |
| static void | |
| render_year(struct Arena *output, struct PageInput input) | |
| { | |
| render_page_color(output, input.hex); | |
| ARENA_APPEND(output, "<title>"); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, " calendar</title>\n"); | |
| render_calendar_style(output, input); | |
| render_randomizer(output, input); | |
| ARENA_APPEND(output, "<main><nav class=year-nav>"); | |
| for (uint32_t distance = 2; distance > 0; distance--) { | |
| if (input.year >= MIN_YEAR + distance) { | |
| render_year_link(output, input, input.year - distance); | |
| } else { | |
| ARENA_APPEND(output, "<span></span>"); | |
| } | |
| } | |
| ARENA_APPEND(output, "<h1><a class=year-link href="); | |
| render_year_url(output, input.hex, input.year); | |
| ARENA_APPEND(output, " id="); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, " aria-current=page>"); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, "</a></h1>"); | |
| for (uint32_t distance = 1; distance <= 2; distance++) { | |
| if (input.year <= MAX_YEAR - distance) { | |
| render_year_link(output, input, input.year + distance); | |
| } else { | |
| ARENA_APPEND(output, "<span></span>"); | |
| } | |
| } | |
| ARENA_APPEND(output, "</nav>\n"); | |
| render_calendar(output, input, 0); | |
| ARENA_APPEND(output, "</main>\n"); | |
| } | |
| static void | |
| render_decade_url(struct Arena *output, uint32_t hex, uint32_t first_year) | |
| { | |
| ARENA_APPEND(output, "/"); | |
| arena_u32_4_digit(output, first_year); | |
| ARENA_APPEND(output, "s"); | |
| ARENA_APPEND(output, "?c="); | |
| arena_u32_hex_6(output, hex); | |
| } | |
| static void | |
| render_decade_link( | |
| struct Arena *output, | |
| uint32_t hex, | |
| uint32_t first_year | |
| ) | |
| { | |
| ARENA_APPEND(output, "<a href="); | |
| render_decade_url(output, hex, first_year); | |
| ARENA_APPEND(output, ">"); | |
| arena_u32_4_digit(output, first_year); | |
| ARENA_APPEND(output, "s</a>"); | |
| } | |
| static void | |
| render_decade(struct Arena *output, struct PageInput input) | |
| { | |
| render_page_color(output, input.hex); | |
| ARENA_APPEND(output, "<title>"); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, "s calendars</title>\n"); | |
| render_calendar_style(output, input); | |
| render_randomizer(output, input); | |
| ARENA_APPEND(output, "<main><nav class=year-nav>"); | |
| if (input.year >= MIN_YEAR + 10) { | |
| render_decade_link(output, input.hex, input.year - 10); | |
| } else { | |
| ARENA_APPEND(output, "<span></span>"); | |
| } | |
| ARENA_APPEND(output, "<h1>"); | |
| arena_u32_4_digit(output, input.year); | |
| ARENA_APPEND(output, "s</h1>"); | |
| if (input.year <= MAX_YEAR - 19) { | |
| render_decade_link(output, input.hex, input.year + 10); | |
| } else { | |
| ARENA_APPEND(output, "<span></span>"); | |
| } | |
| ARENA_APPEND(output, "</nav>\n"); | |
| for (uint32_t year = input.year; year <= input.year + 9; year++) { | |
| if (year < MIN_YEAR || year > MAX_YEAR) continue; | |
| struct PageInput calendar_input = input; | |
| calendar_input.year = year; | |
| ARENA_APPEND(output, "<section class=decade-calendar><h2 id="); | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "><a href="); | |
| render_year_url(output, input.hex, year); | |
| ARENA_APPEND(output, ">"); | |
| arena_u32_4_digit(output, year); | |
| ARENA_APPEND(output, "</a></h2>\n"); | |
| render_calendar(output, calendar_input, 1); | |
| ARENA_APPEND(output, "</section>\n"); | |
| } | |
| ARENA_APPEND(output, "</main>\n"); | |
| } | |
| static int | |
| route_brand_redirect( | |
| const char *path, | |
| size_t path_size, | |
| struct PageInput *page | |
| ) | |
| { | |
| if (path_size == 0 || path[0] < 'a' || path[0] > 'z') return 0; | |
| if (path_equals(path, path_size, PASS_STR("apple.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1976; | |
| page->month = 4; | |
| page->day = 1; | |
| page->hex = random_apple_color(page->random_seed); | |
| } else if (path_equals(path, path_size, PASS_STR("daringfireball.net"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2002; | |
| page->month = 8; | |
| page->day = 13; | |
| page->hex = 0x4a525a; | |
| } else if (path_equals(path, path_size, PASS_STR("microsoft.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1975; | |
| page->month = 4; | |
| page->day = 4; | |
| page->hex = 0x00a4ef; | |
| } else if (path_equals(path, path_size, PASS_STR("xbox.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2001; | |
| page->month = 11; | |
| page->day = 15; | |
| page->hex = 0x107c10; | |
| } else if (path_equals(path, path_size, PASS_STR("chatgpt.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2022; | |
| page->month = 11; | |
| page->day = 30; | |
| page->hex = 0x10a37f; | |
| } else if (path_equals(path, path_size, PASS_STR("claude.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2023; | |
| page->month = 3; | |
| page->day = 14; | |
| page->hex = 0xd97757; | |
| } else if (path_equals(path, path_size, PASS_STR("stripe.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2011; | |
| page->month = 9; | |
| page->day = 29; | |
| page->hex = 0x635bff; | |
| } else if (path_equals(path, path_size, PASS_STR("paulgraham.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1964; | |
| page->month = 11; | |
| page->day = 13; | |
| page->hex = 0xffffff; | |
| } else if (path_equals(path, path_size, PASS_STR("gatesnotes.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1955; | |
| page->month = 10; | |
| page->day = 28; | |
| page->hex = 0x0067b8; | |
| } else if (path_equals(path, path_size, PASS_STR("wikipedia.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2001; | |
| page->month = 1; | |
| page->day = 15; | |
| page->hex = 0x3366cc; | |
| } else if (path_equals(path, path_size, PASS_STR("youtube.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2005; | |
| page->month = 4; | |
| page->day = 23; | |
| page->hex = 0xff0033; | |
| } else if (path_equals(path, path_size, PASS_STR("github.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2008; | |
| page->month = 4; | |
| page->day = 10; | |
| page->hex = 0x0d1117; | |
| } else if (path_equals(path, path_size, PASS_STR("wordpress.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2003; | |
| page->month = 5; | |
| page->day = 27; | |
| page->hex = 0x21759b; | |
| } else if (path_equals(path, path_size, PASS_STR("nytimes.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1851; | |
| page->month = 9; | |
| page->day = 18; | |
| page->hex = 0x000000; | |
| } else if (path_equals(path, path_size, PASS_STR("economist.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1843; | |
| page->month = 9; | |
| page->day = 2; | |
| page->hex = 0xe3120b; | |
| } else if (path_equals(path, path_size, PASS_STR("ft.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1888; | |
| page->month = 1; | |
| page->day = 10; | |
| page->hex = 0xfff1e5; | |
| } else if (path_equals(path, path_size, PASS_STR("bloomberg.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1981; | |
| page->month = 10; | |
| page->day = 1; | |
| page->hex = 0xf5a623; | |
| } else if (path_equals(path, path_size, PASS_STR("theguardian.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1821; | |
| page->month = 5; | |
| page->day = 5; | |
| page->hex = 0x052962; | |
| } else if (path_equals(path, path_size, PASS_STR("info.cern.ch"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1991; | |
| page->month = 8; | |
| page->day = 6; | |
| page->hex = 0x0033a0; | |
| } else if (path_equals(path, path_size, PASS_STR("google.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1998; | |
| page->month = 9; | |
| page->day = 27; | |
| page->hex = random_google_color(page->random_seed); | |
| } else if (path_equals(path, path_size, PASS_STR("ebay.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1995; | |
| page->month = 9; | |
| page->day = 3; | |
| page->hex = random_ebay_color(page->random_seed); | |
| } else if (path_equals(path, path_size, PASS_STR("cocacola.com")) | |
| || path_equals(path, path_size, PASS_STR("coca-cola.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1886; | |
| page->month = 5; | |
| page->day = 8; | |
| page->hex = 0xf40009; | |
| } else if (path_equals(path, path_size, PASS_STR("pepsi.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1898; | |
| page->month = 8; | |
| page->day = 28; | |
| page->hex = 0x004b93; | |
| } else if (path_equals(path, path_size, PASS_STR("nintendo.com")) | |
| || path_equals(path, path_size, PASS_STR("nintendo.co.jp"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1889; | |
| page->month = 9; | |
| page->day = 23; | |
| page->hex = 0xe60012; | |
| } else if (path_equals(path, path_size, PASS_STR("pokemon.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1996; | |
| page->month = 2; | |
| page->day = 27; | |
| page->hex = 0xffcb05; | |
| } else if (path_equals(path, path_size, PASS_STR("nokia.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1865; | |
| page->month = 5; | |
| page->day = 12; | |
| page->hex = 0x124191; | |
| } else if (path_equals(path, path_size, PASS_STR("sony.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1946; | |
| page->month = 5; | |
| page->day = 7; | |
| page->hex = 0x000000; | |
| } else if (path_equals(path, path_size, PASS_STR("playstation.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1994; | |
| page->month = 12; | |
| page->day = 3; | |
| page->hex = 0x0070d1; | |
| } else if (path_equals(path, path_size, PASS_STR("tsmc.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1987; | |
| page->month = 2; | |
| page->day = 21; | |
| page->hex = 0xed1c24; | |
| } else if (path_equals(path, path_size, PASS_STR("nvidia.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1993; | |
| page->month = 4; | |
| page->day = 5; | |
| page->hex = 0x76b900; | |
| } else if (path_equals(path, path_size, PASS_STR("intel.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1968; | |
| page->month = 7; | |
| page->day = 18; | |
| page->hex = 0x0071c5; | |
| } else if (path_equals(path, path_size, PASS_STR("amd.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1969; | |
| page->month = 5; | |
| page->day = 1; | |
| page->hex = 0xed1c24; | |
| } else if (path_equals(path, path_size, PASS_STR("ibm.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1911; | |
| page->month = 6; | |
| page->day = 16; | |
| page->hex = 0x0f62fe; | |
| } else if (path_equals(path, path_size, PASS_STR("samsung.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1938; | |
| page->month = 3; | |
| page->day = 1; | |
| page->hex = 0x1428a0; | |
| } else if (path_equals(path, path_size, PASS_STR("hp.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1939; | |
| page->month = 1; | |
| page->day = 1; | |
| page->hex = 0x0096d6; | |
| } else if (path_equals(path, path_size, PASS_STR("meta.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2004; | |
| page->month = 2; | |
| page->day = 4; | |
| page->hex = 0x0866ff; | |
| } else if (path_equals(path, path_size, PASS_STR("netflix.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1997; | |
| page->month = 8; | |
| page->day = 29; | |
| page->hex = 0xe50914; | |
| } else if (path_equals(path, path_size, PASS_STR("atari.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1972; | |
| page->month = 6; | |
| page->day = 27; | |
| page->hex = 0xe4202e; | |
| } else if (path_equals(path, path_size, PASS_STR("amazon.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1995; | |
| page->month = 7; | |
| page->day = 16; | |
| page->hex = 0xff9900; | |
| } else if (path_equals(path, path_size, PASS_STR("aws.amazon.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2006; | |
| page->month = 3; | |
| page->day = 14; | |
| page->hex = 0x232f3e; | |
| } else if (path_equals(path, path_size, PASS_STR("news.ycombinator.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2007; | |
| page->month = 2; | |
| page->day = 19; | |
| page->hex = 0xff6600; | |
| } else if (path_equals(path, path_size, PASS_STR("reddit.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2005; | |
| page->month = 6; | |
| page->day = 23; | |
| page->hex = 0xff4500; | |
| } else if (path_equals(path, path_size, PASS_STR("stackoverflow.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2008; | |
| page->month = 9; | |
| page->day = 15; | |
| page->hex = 0xf48024; | |
| } else if (path_equals(path, path_size, PASS_STR("firefox.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2004; | |
| page->month = 11; | |
| page->day = 9; | |
| page->hex = 0xff7139; | |
| } else if (path_equals(path, path_size, PASS_STR("python.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1991; | |
| page->month = 2; | |
| page->day = 20; | |
| page->hex = random_python_color(page->random_seed); | |
| } else if (path_equals(path, path_size, PASS_STR("ruby-lang.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1995; | |
| page->month = 12; | |
| page->day = 21; | |
| page->hex = 0xcc342d; | |
| } else if (path_equals(path, path_size, PASS_STR("nodejs.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2009; | |
| page->month = 5; | |
| page->day = 27; | |
| page->hex = 0x339933; | |
| } else if (path_equals(path, path_size, PASS_STR("react.dev"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2013; | |
| page->month = 5; | |
| page->day = 29; | |
| page->hex = 0x61dafb; | |
| } else if (path_equals(path, path_size, PASS_STR("rust-lang.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2015; | |
| page->month = 5; | |
| page->day = 15; | |
| page->hex = 0x000000; | |
| } else if (path_equals(path, path_size, PASS_STR("golang.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2009; | |
| page->month = 11; | |
| page->day = 10; | |
| page->hex = 0x00add8; | |
| } else if (path_equals(path, path_size, PASS_STR("postgresql.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1996; | |
| page->month = 7; | |
| page->day = 8; | |
| page->hex = 0x336791; | |
| } else if (path_equals(path, path_size, PASS_STR("stevejobs.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1955; | |
| page->month = 2; | |
| page->day = 24; | |
| page->hex = 0x000000; | |
| } else if (path_equals(path, path_size, PASS_STR("timbl.com"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1955; | |
| page->month = 6; | |
| page->day = 8; | |
| page->hex = 0x008000; | |
| } else if (path_equals(path, path_size, PASS_STR("linux.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 1991; | |
| page->month = 8; | |
| page->day = 25; | |
| page->hex = 0xffcc33; | |
| } else if (path_equals(path, path_size, PASS_STR("elixir-lang.org"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2012; | |
| page->month = 5; | |
| page->day = 25; | |
| page->hex = 0x4e2a8e; | |
| } else if (path_equals(path, path_size, PASS_STR("atp.fm"))) { | |
| page->kind = PAGE_BRAND_REDIRECT; | |
| page->year = 2013; | |
| page->month = 3; | |
| page->day = 11; | |
| page->hex = random_atp_color(page->random_seed); | |
| } | |
| return page->kind == PAGE_BRAND_REDIRECT; | |
| } | |
| static struct Route | |
| route_path(const char *path, size_t path_size, uint32_t random_seed) | |
| { | |
| struct Route route = { | |
| .kind = ROUTE_NOT_FOUND, | |
| .page = { | |
| .kind = PAGE_NOT_FOUND, | |
| .random_seed = random_seed, | |
| }, | |
| }; | |
| if (path_size == 0) { | |
| route.kind = ROUTE_ROOT; | |
| } else if (path_equals(path, path_size, PASS_STR("robots.txt"))) { | |
| route.kind = ROUTE_ROBOTS; | |
| } else if (route_brand_redirect(path, path_size, &route.page)) { | |
| route.kind = ROUTE_BRAND_REDIRECT; | |
| } else if (parse_year_4(path, path_size, &route.page.year)) { | |
| route.kind = ROUTE_YEAR; | |
| } else if (parse_decade(path, path_size, &route.page.year)) { | |
| route.kind = ROUTE_DECADE; | |
| } | |
| return route; | |
| } | |
| static size_t | |
| render_page(struct PageInput input, struct Arena *output) | |
| { | |
| char *start = output->ptr; | |
| ARENA_APPEND(output, | |
| "<!doctype html><html lang=en><meta charset=utf-8>" | |
| "<meta name=viewport content=\"width=device-width,initial-scale=1\">" | |
| "<style>\n" | |
| "@view-transition { navigation: auto; }\n" | |
| ":root {\n" | |
| "--bg: #000000;\n" | |
| "--page-transition-duration: .5s;\n" | |
| "--button-transition-duration: .3s;\n" | |
| "background: var(--bg); color: contrast-color(var(--bg));" | |
| "font-family: sans-serif; line-height: 1.5; scroll-behavior: smooth;" | |
| "}\n" | |
| "@media (prefers-reduced-motion: reduce) { :root { --page-transition-duration: 0s; --button-transition-duration: 0s; scroll-behavior: auto; } }\n" | |
| "* { font-size: 1rem; }\n" | |
| "body { margin: 0; text-align: center; }\n" | |
| "a { color: inherit }\n" | |
| ".randomizer { position: fixed; bottom: 1rem; left: 1rem; z-index: 3; margin: 0; }\n" | |
| ".visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }\n" | |
| "button { appearance: none; padding: 0; border: 0; background: transparent; color: inherit; font: inherit; cursor: pointer; opacity: 1; transition: opacity var(--button-transition-duration) ease-out; view-transition-name: random-color-button; }\n" | |
| ":root:active-view-transition button { visibility: hidden; opacity: 0; }\n" | |
| "@keyframes random-color-button-out { to { opacity: 0; } }\n" | |
| "::view-transition-old(random-color-button) { animation: var(--button-transition-duration) ease-out both random-color-button-out; }\n" | |
| "::view-transition-new(random-color-button) { visibility: hidden; }\n" | |
| "</style>\n" | |
| ); | |
| switch (input.kind) { | |
| case PAGE_NOT_FOUND: | |
| render_not_found(output, input); | |
| break; | |
| case PAGE_CURRENT_YEAR_REDIRECT: | |
| case PAGE_DECADE_REDIRECT: | |
| case PAGE_BRAND_REDIRECT: | |
| break; | |
| case PAGE_COLOR_YEAR: | |
| render_year(output, input); | |
| break; | |
| case PAGE_COLOR_DECADE: | |
| render_decade(output, input); | |
| break; | |
| } | |
| return (size_t)(output->ptr - start); | |
| } | |
| int | |
| start_server(int port) | |
| { | |
| int server_fd = socket(AF_INET, SOCK_STREAM, 0); | |
| if (server_fd < 0) GAME_OVER("Cannot open socket."); | |
| struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(port) }; | |
| if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) <= 0) GAME_OVER("Cannot convert address 127.0.0.1."); | |
| int yes = 1; | |
| setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); | |
| if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | |
| perror("bind"); | |
| GAME_OVER("Cannot bind to address."); | |
| } | |
| if (listen(server_fd, 64) < 0) GAME_OVER("Cannot listen."); | |
| return server_fd; | |
| } | |
| int main() { | |
| int server_fd = start_server(9000); | |
| int client_fd = -1; | |
| uint32_t random_state = initial_random_seed(); | |
| do { | |
| // if (client_fd >= 0) close(client_fd); | |
| client_fd = accept(server_fd, NULL, NULL); | |
| if (client_fd < 0) continue; | |
| struct timeval request_timeout = { | |
| .tv_sec = 0, | |
| .tv_usec = 250000, | |
| }; | |
| if (setsockopt( | |
| client_fd, | |
| SOL_SOCKET, | |
| SO_RCVTIMEO, | |
| &request_timeout, | |
| sizeof(request_timeout) | |
| ) < 0) { | |
| perror("setsockopt SO_RCVTIMEO"); | |
| } | |
| char request_buffer[REQUEST_CAP] = {0}; | |
| const char *request = (char *)request_buffer; | |
| ssize_t request_size = read_request_headers( | |
| client_fd, | |
| request_buffer, | |
| sizeof(request_buffer) | |
| ); | |
| if (request_size <= 0) { | |
| close(client_fd); | |
| client_fd = -1; | |
| continue; | |
| } | |
| int client_errors = 0; | |
| int query_errors = 0; | |
| const char *get_prefix = "GET /"; | |
| size_t get_size = strlen(get_prefix); | |
| size_t path_size = 0; | |
| int is_get = strncmp(request, get_prefix, get_size) == 0; | |
| random_state = random_u32(random_state); | |
| struct Route route = { | |
| .kind = ROUTE_NOT_FOUND, | |
| .page = { | |
| .kind = PAGE_NOT_FOUND, | |
| .random_seed = random_state, | |
| }, | |
| }; | |
| /* | |
| * Resolve the path independently of the query: | |
| * known page -> process its query; redirect path -> 307; unknown path -> 404. | |
| * For a known page, the query then produces: | |
| * valid and complete -> 200; malformed recognized input -> 400; | |
| * missing canonical state (such as c) -> 307; unknown input -> ignored. | |
| * In particular, a malformed query must never turn a known path into a 404. | |
| */ | |
| if (is_get) { | |
| // Move parser ahead | |
| request += get_size; | |
| request_size -= get_size; | |
| char *space = memchr(request, ' ', request_size); | |
| if (!space) client_errors++; | |
| path_size = space ? space - request : 0; | |
| char *query = memchr(request, '?', path_size); | |
| size_t query_size = query ? (size_t)(space - query - 1) : 0; | |
| path_size = query ? (size_t)(query - request) : path_size; | |
| // Path routing happens before the query is parsed. | |
| route = route_path(request, path_size, random_state); | |
| int is_page_route = route.kind == ROUTE_ROOT | |
| || route.kind == ROUTE_ROBOTS | |
| || route.kind == ROUTE_YEAR | |
| || route.kind == ROUTE_DECADE; | |
| struct QueryInput query_input = {0}; | |
| if (is_page_route && !parse_query( | |
| query ? query + 1 : request, | |
| query_size, | |
| &query_input | |
| )) { | |
| query_errors++; | |
| } | |
| if (route.kind == ROUTE_ROOT) { | |
| route.page.kind = PAGE_CURRENT_YEAR_REDIRECT; | |
| route.page.year = local_year_now(); | |
| route.page.hex = query_input.has_hex | |
| ? query_input.hex | |
| : random_hsl_color(route.page.random_seed); | |
| } else if (route.kind == ROUTE_YEAR) { | |
| route.page.hex = query_input.has_hex | |
| ? query_input.hex | |
| : random_hsl_color(route.page.random_seed); | |
| route.page.kind = query_input.has_hex | |
| ? PAGE_COLOR_YEAR | |
| : PAGE_CURRENT_YEAR_REDIRECT; | |
| if (query_input.has_previous_year) { | |
| route.page.calendar_direction = year_direction( | |
| route.page.year, | |
| query_input.previous_year | |
| ); | |
| } | |
| } else if (route.kind == ROUTE_DECADE) { | |
| route.page.hex = query_input.has_hex | |
| ? query_input.hex | |
| : random_hsl_color(route.page.random_seed); | |
| route.page.kind = query_input.has_hex | |
| ? PAGE_COLOR_DECADE | |
| : PAGE_DECADE_REDIRECT; | |
| } | |
| } | |
| struct PageInput page = route.page; | |
| int is_robots_request = route.kind == ROUTE_ROBOTS; | |
| int is_unknown_path = route.kind == ROUTE_NOT_FOUND; | |
| if (is_unknown_path) { | |
| client_errors++; | |
| } else if (route.kind != ROUTE_BRAND_REDIRECT) { | |
| client_errors += query_errors; | |
| } | |
| char body_arena_buffer[OUTPUT_CAP]; | |
| struct Arena body_arena = { | |
| .ptr = body_arena_buffer, | |
| .remaining = sizeof(body_arena_buffer), | |
| }; | |
| int is_redirect = page.kind == PAGE_CURRENT_YEAR_REDIRECT | |
| || page.kind == PAGE_DECADE_REDIRECT | |
| || page.kind == PAGE_BRAND_REDIRECT; | |
| size_t body_size = 0; | |
| if (is_robots_request && client_errors == 0) { | |
| ARENA_APPEND(&body_arena, "User-agent: *\nAllow: /\n"); | |
| body_size = (size_t)(body_arena.ptr - body_arena_buffer); | |
| } else if (!is_redirect && (client_errors == 0 || is_unknown_path)) { | |
| body_size = render_page(page, &body_arena); | |
| } | |
| if (client_errors > 0) { | |
| if (is_unknown_path) { | |
| write_404_not_found(client_fd); | |
| } else { | |
| write_400_bad_request(client_fd); | |
| } | |
| } else if (page.kind == PAGE_CURRENT_YEAR_REDIRECT) { | |
| write_307_temporary_redirect(client_fd); | |
| } else if (page.kind == PAGE_DECADE_REDIRECT) { | |
| write_307_temporary_redirect(client_fd); | |
| } else if (page.kind == PAGE_BRAND_REDIRECT) { | |
| write_307_temporary_redirect(client_fd); | |
| } else { | |
| write_200_ok(client_fd); | |
| } | |
| if (client_errors == 0 && page.kind == PAGE_CURRENT_YEAR_REDIRECT) { | |
| dprintf( | |
| client_fd, | |
| "Location: /%04" PRIu32 "?c=%06" PRIx32 "\r\n", | |
| page.year, | |
| page.hex | |
| ); | |
| } else if (client_errors == 0 && page.kind == PAGE_DECADE_REDIRECT) { | |
| dprintf( | |
| client_fd, | |
| "Location: /%04" PRIu32 "s?c=%06" PRIx32 "\r\n", | |
| page.year, | |
| page.hex | |
| ); | |
| } else if (client_errors == 0 && page.kind == PAGE_BRAND_REDIRECT) { | |
| dprintf( | |
| client_fd, | |
| "Location: https://allweeks.exe.xyz/%04" PRIu32 | |
| "?c=%06" PRIx32 "#%04" PRIu32 "-%02" PRIu8 "-%02" PRIu8 "\r\n", | |
| page.year, | |
| page.hex, | |
| page.year, | |
| page.month, | |
| page.day | |
| ); | |
| } | |
| write(client_fd, PASS_STR("Connection: close\r\n")); | |
| if (is_robots_request) { | |
| write(client_fd, PASS_STR("Content-Type: text/plain; charset=utf-8\r\n")); | |
| } else { | |
| write(client_fd, PASS_STR("Content-Type: text/html\r\n")); | |
| } | |
| write(client_fd, PASS_STR( | |
| "Content-Security-Policy: default-src 'none'; script-src 'none'; " | |
| "style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'\r\n" | |
| )); | |
| dprintf(client_fd, "Content-Length: %zu\r\n\r\n", body_size); | |
| if (!write_all(client_fd, body_arena_buffer, body_size)) { | |
| perror("write"); | |
| } | |
| close(client_fd); | |
| client_fd = -1; | |
| } while (1); | |
| close(server_fd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment