Perform some minor code cleanup

This commit is contained in:
Justine Tunney 2021-04-06 12:46:52 -07:00
parent 6c16f208b5
commit 59575f7e80
4 changed files with 25 additions and 35 deletions

View file

@ -1397,19 +1397,19 @@ long: push $GDT_LONG_DATA
jmp *%rax jmp *%rax
.endfn long .endfn long
/* /*

View file

@ -16,13 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/fmt/conv.h" #include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/prio.h" #include "libc/sysv/consts/prio.h"
static int clamp(int p) { static int clamp(int p) {
return max(-NZERO, min(NZERO - 1, p)); return MAX(-NZERO, MIN(NZERO - 1, p));
} }
/** /**
@ -34,7 +34,7 @@ static int clamp(int p) {
*/ */
int nice(int delta) { int nice(int delta) {
int p; int p;
if (abs(delta) >= NZERO * 2) { if (ABS(delta) >= NZERO * 2) {
p = delta; p = delta;
} else { } else {
delta = clamp(delta); delta = clamp(delta);

View file

@ -157,8 +157,6 @@ USAGE\n\
#define HASH_LOAD_FACTOR /* 1. / */ 4 #define HASH_LOAD_FACTOR /* 1. / */ 4
#define DEFAULT_PORT 8080 #define DEFAULT_PORT 8080
#define AppendHeaderName(p, s) stpcpy(stpcpy(p, s), ": ")
static const struct itimerval kHeartbeat = { static const struct itimerval kHeartbeat = {
{0, 500000}, {0, 500000},
{0, 500000}, {0, 500000},
@ -1159,14 +1157,6 @@ static void *AddRange(char *content, long start, long length) {
} }
} }
static bool IsConnectionClose(void) {
int n;
char *p;
p = inbuf.p + msg.headers[kHttpConnection].a;
n = msg.headers[kHttpConnection].b - msg.headers[kHttpConnection].a;
return n == 5 && memcmp(p, "close", 5) == 0;
}
static char *AppendCrlf(char *p) { static char *AppendCrlf(char *p) {
p[0] = '\r'; p[0] = '\r';
p[1] = '\n'; p[1] = '\n';
@ -1193,11 +1183,11 @@ static char *SetStatus(int code, const char *reason) {
static char *AppendHeader(char *p, const char *k, const char *v) { static char *AppendHeader(char *p, const char *k, const char *v) {
if (!v) return p; if (!v) return p;
return AppendCrlf(stpcpy(AppendHeaderName(p, k), v)); return AppendCrlf(stpcpy(stpcpy(stpcpy(p, k), ": "), v));
} }
static char *AppendContentType(char *p, const char *ct) { static char *AppendContentType(char *p, const char *ct) {
p = AppendHeaderName(p, "Content-Type"); p = stpcpy(p, "Content-Type: ");
p = stpcpy(p, ct); p = stpcpy(p, ct);
if (startswith(ct, "text/") && !strchr(ct, ';')) { if (startswith(ct, "text/") && !strchr(ct, ';')) {
p = stpcpy(p, "; charset=utf-8"); p = stpcpy(p, "; charset=utf-8");
@ -1223,7 +1213,7 @@ static char *ServeError(int code, const char *reason) {
static char *AppendExpires(char *p, int64_t t) { static char *AppendExpires(char *p, int64_t t) {
struct tm tm; struct tm tm;
gmtime_r(&t, &tm); gmtime_r(&t, &tm);
p = AppendHeaderName(p, "Expires"); p = stpcpy(p, "Expires: ");
p = FormatHttpDateTime(p, &tm); p = FormatHttpDateTime(p, &tm);
return AppendCrlf(p); return AppendCrlf(p);
} }
@ -1231,7 +1221,7 @@ static char *AppendExpires(char *p, int64_t t) {
static char *AppendCache(char *p, int64_t seconds) { static char *AppendCache(char *p, int64_t seconds) {
struct tm tm; struct tm tm;
if (seconds < 0) return p; if (seconds < 0) return p;
p = AppendHeaderName(p, "Cache-Control"); p = stpcpy(p, "Cache-Control: ");
p = stpcpy(p, "max-age="); p = stpcpy(p, "max-age=");
p += uint64toarray_radix10(seconds, p); p += uint64toarray_radix10(seconds, p);
if (seconds) p = stpcpy(p, ", public"); if (seconds) p = stpcpy(p, ", public");
@ -1240,7 +1230,7 @@ static char *AppendCache(char *p, int64_t seconds) {
} }
static char *AppendContentLength(char *p, size_t n) { static char *AppendContentLength(char *p, size_t n) {
p = AppendHeaderName(p, "Content-Length"); p = stpcpy(p, "Content-Length: ");
p += uint64toarray_radix10(n, p); p += uint64toarray_radix10(n, p);
return AppendCrlf(p); return AppendCrlf(p);
} }
@ -1252,8 +1242,7 @@ static char *AppendContentRange(char *p, long rangestart, long rangelength,
CHECK_GT(rangestart + rangelength, rangestart); CHECK_GT(rangestart + rangelength, rangestart);
CHECK_LE(rangestart + rangelength, contentlength); CHECK_LE(rangestart + rangelength, contentlength);
endrange = rangestart + rangelength - 1; endrange = rangestart + rangelength - 1;
p = AppendHeaderName(p, "Content-Range"); p = stpcpy(p, "Content-Range: bytes ");
p = stpcpy(p, "bytes ");
p += uint64toarray_radix10(rangestart, p); p += uint64toarray_radix10(rangestart, p);
*p++ = '-'; *p++ = '-';
p += uint64toarray_radix10(endrange, p); p += uint64toarray_radix10(endrange, p);
@ -2368,7 +2357,7 @@ static char *HandleMessage(void) {
} }
msgsize = need; /* we are now synchronized */ msgsize = need; /* we are now synchronized */
LogBody("received", inbuf.p + hdrsize, msgsize - hdrsize); LogBody("received", inbuf.p + hdrsize, msgsize - hdrsize);
if (httpversion != 101 || IsConnectionClose()) { if (httpversion != 101 || !CompareHeader(kHttpConnection, "close")) {
connectionclose = true; connectionclose = true;
} }
ParseRequestUri(); ParseRequestUri();

View file

@ -467,7 +467,8 @@ static int ReadAll(int fd, void *data, size_t size) {
n = size; n = size;
do { do {
if ((rc = read(fd, p, n)) == -1) return -1; if ((rc = read(fd, p, n)) == -1) return -1;
assert((got = rc) || !n); got = rc;
assert(got || !n);
p += got; p += got;
n -= got; n -= got;
} while (n); } while (n);