C302/c302.c

34 lines
1.0 KiB
C
Raw Normal View History

2022-11-23 15:43:20 +09:00
#include <stdint.h>
2022-11-23 15:21:23 +09:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <time.h>
#include "c302.h"
2022-11-23 15:45:22 +09:00
static void http_error(errcode_enum_t code, char* msg) {
2022-11-23 15:21:23 +09:00
uint32_t len = strlen(msg) + typel[code];
char* str = malloc(len);
sprintf(str, types[code], msg);
const struct iovec iov[2] = {{&len, sizeof(uint32_t)}, {str, len}};
writev(1, (const struct iovec *)&iov, 2);
free(str);
}
// Usage: c302 method query_string
int main(int argc, char **argv) {
if (argc != 3) {
http_error(HTTP500, "Argument Count Error.");
return 1;
}
2024-04-29 15:54:48 +09:00
int i = time(NULL) % (sizeof(urls)/sizeof(char*));
2022-11-23 16:37:12 +09:00
uint32_t len = sizeof(HTTP302HEAD)-1 + urlsl[i] + 4 + sizeof(CONTENT)-1;
2022-11-29 14:19:28 +09:00
const struct iovec iov[4] = {
2022-11-23 16:37:12 +09:00
{(void *)&len, sizeof(uint32_t)},
{HTTP302HEAD, sizeof(HTTP302HEAD)-1},
{(void *)urls[i], urlsl[i]},
2022-11-29 14:19:28 +09:00
{"\r\n\r\n" CONTENT, 4+sizeof(CONTENT)-1}
2022-11-23 16:37:12 +09:00
};
2022-11-29 14:19:28 +09:00
return writev(1, (const struct iovec *)&iov, 4) != len+sizeof(uint32_t);
2022-11-23 15:21:23 +09:00
}