Skip to main content
Commonmark migration
Source Link

Update

#Update II found some time and a few other things in the code.

#Update I found some time and a few other things in the code.

Update

I found some time and a few other things in the code.

fixed typo
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
size_t bytes;
do {
    bytes = fread(buffer, sizeof(char), BLOCK_SIZE, stdin);
    fwrite(buffer, sizeof(char), bytes, stdout);1;
} while (bytes == BLOCK_SIZE && !feof(stdin));
size_t bytes;
do {
    bytes = fread(buffer, sizeof(char), BLOCK_SIZE, stdin);
    fwrite(buffer, sizeof(char), bytes, stdout);1
} while (bytes == BLOCK_SIZE && !feof(stdin));
size_t bytes;
do {
    bytes = fread(buffer, sizeof(char), BLOCK_SIZE, stdin);
    fwrite(buffer, sizeof(char), bytes, stdout);
} while (bytes == BLOCK_SIZE && !feof(stdin));
added some tips
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

#Update I found some time and a few other things in the code.

Refactor parseStatus

The current parseStatus routine does more work than needed to extract the status code. You don't need to copy the string to pass it to atoi so it can be greatly simplified like so:

static int parseStatus(char* line) {
    char* http  = strstr(line, "HTTP/1.");
    if (http) {
        http = strchr(http, ' ');
        if (http) {
            return atoi(http);
        }
    }
    return 0;
}

Refactor getHeaderName

Right now, the getHeaderName function is outlined something like this:

static char* getHeaderName(char* line) {
    static char header[MAX_HEADER_LENGTH];
    /* ... */
    return header;
}

This works (and even some ancient library functions were done this way) but it's not thread-safe and the design can easily be improved. Instead, it's better to have the calling function allocate a buffer and pass it in, along with the length rather than pointing to an internal static buffer.

Refactor isDelimited

In a similar vein, your isDelimited function is really only checking for two particular bytes, so it would be more clear and slightly faster to check for them directly instead of constructing a copy in memory and then using strcmp.

#Update I found some time and a few other things in the code.

Refactor parseStatus

The current parseStatus routine does more work than needed to extract the status code. You don't need to copy the string to pass it to atoi so it can be greatly simplified like so:

static int parseStatus(char* line) {
    char* http  = strstr(line, "HTTP/1.");
    if (http) {
        http = strchr(http, ' ');
        if (http) {
            return atoi(http);
        }
    }
    return 0;
}

Refactor getHeaderName

Right now, the getHeaderName function is outlined something like this:

static char* getHeaderName(char* line) {
    static char header[MAX_HEADER_LENGTH];
    /* ... */
    return header;
}

This works (and even some ancient library functions were done this way) but it's not thread-safe and the design can easily be improved. Instead, it's better to have the calling function allocate a buffer and pass it in, along with the length rather than pointing to an internal static buffer.

Refactor isDelimited

In a similar vein, your isDelimited function is really only checking for two particular bytes, so it would be more clear and slightly faster to check for them directly instead of constructing a copy in memory and then using strcmp.

Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading