diff options
| author | Nakidai <nakidai@disroot.org> | 2024-11-24 18:40:16 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2024-11-24 18:40:16 +0300 |
| commit | 6a969267a42280cff5826da1749322056e6dad84 (patch) | |
| tree | 0995ad4789098634ecfa0707a728208a26894e3d | |
| parent | 1076352c69ca4f62adfe8df653c23454597ffc4e (diff) | |
| download | libhttpc-6a969267a42280cff5826da1749322056e6dad84.tar.gz libhttpc-6a969267a42280cff5826da1749322056e6dad84.zip | |
Make headers a usual array
| -rw-r--r-- | include/libhttpc.h | 32 | ||||
| -rw-r--r-- | src/header.c | 4 | ||||
| -rw-r--r-- | src/request.c | 21 | ||||
| -rw-r--r-- | src/response.c | 4 |
4 files changed, 29 insertions, 32 deletions
diff --git a/include/libhttpc.h b/include/libhttpc.h index 08d4b80..87f6e6c 100644 --- a/include/libhttpc.h +++ b/include/libhttpc.h @@ -20,7 +20,7 @@ typedef void *LibHTTPC_Realloc(void *, size_t); /** * Enum that contains all headers that are supported by the HTTP/1.1 */ -enum LibHTTPC_Header +enum LibHTTPC_HeaderType { /* General headers */ LibHTTPC_Header_CACHE_CONTROL, @@ -146,20 +146,27 @@ enum LibHTTPC_Status LibHTTPC_Status_HTTP_VER_NOT_SUPPORTED = 505, }; +struct LibHTTPC_Header +{ + const char *name; + const char *value; +}; + /** * Struct that contains parsed request. * @see LibHTTPC_loadRequest */ struct LibHTTPC_Request { - char *buf; /**< Pointer to char *request */ - char *method, *uri, *version; /**< Request line */ - char **header_names; /**< \ Header parallel array */ - char **header_values; /**< / Header parallel array */ - char *body; /**< Pointer to body */ - size_t header_count; /**< Length of header array */ - int selfalloc; /**< Marker if request was allocated by the library */ - int header_selfalloc; /**< Marker if header array was allocated by the library */ + char *buf; /**< Pointer to char *request */ + char *method; /**< Request method */ + char *uri; /**< Request URI */ + char *version; /**< HTTP version */ + struct LibHTTPC_Header *headers; /**< Header array */ + char *body; /**< Pointer to body */ + size_t header_count; /**< Length of header array */ + int selfalloc; /**< Marker if request was allocated by the library */ + int header_selfalloc; /**< Marker if header array was allocated by the library */ }; /** @@ -172,8 +179,7 @@ struct LibHTTPC_Response const char *version; /**< HTTP version */ enum LibHTTPC_Status status; /**< Status code */ const char *phrase; /**< Phrase describing status code */ - const char **header_names; /**< \ Header parallel array */ - const char **header_values; /**< / Header parallel array */ + struct LibHTTPC_Header *headers; /**< Header array */ const char *body; /**< Pointer to body */ size_t header_count; /**< Length of header array */ }; @@ -197,7 +203,7 @@ void LibHTTPC(LibHTTPC_Malloc *malloc, LibHTTPC_Realloc *realloc); /** * Parse header name */ -enum LibHTTPC_Header LibHTTPC_loadHeader(const char *header); +enum LibHTTPC_HeaderType LibHTTPC_loadHeader(const char *header); /** * Parse method name */ @@ -206,7 +212,7 @@ enum LibHTTPC_Method LibHTTPC_loadMethod(const char *method); /** * Get name of the header by enum LibHTTPC_Header */ -const char *LibHTTPC_dumpHeader(enum LibHTTPC_Header header); +const char *LibHTTPC_dumpHeader(enum LibHTTPC_HeaderType header); /** * Get name of the header by enum LibHTTPC_Method */ diff --git a/src/header.c b/src/header.c index 293f435..f3072ad 100644 --- a/src/header.c +++ b/src/header.c @@ -121,7 +121,7 @@ static int strieq(const char *a, const char *b) CASE(LAST_MODIFIED); \ } while(0) -enum LibHTTPC_Header LibHTTPC_loadHeader(const char *header) +enum LibHTTPC_HeaderType LibHTTPC_loadHeader(const char *header) { #define CASE(X) if (!strieq((S_##X), header)) return HEADER(X) CHECK(); @@ -129,7 +129,7 @@ enum LibHTTPC_Header LibHTTPC_loadHeader(const char *header) return HEADER(EXTENSION_HEADER); } -const char *LibHTTPC_dumpHeader(enum LibHTTPC_Header header) +const char *LibHTTPC_dumpHeader(enum LibHTTPC_HeaderType header) { switch (header) { diff --git a/src/request.c b/src/request.c index 6885b1f..3e017b7 100644 --- a/src/request.c +++ b/src/request.c @@ -53,23 +53,17 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b if (selfmalloc) { ++request_buf->header_count; - - if (!request_buf->header_names) - request_buf->header_names = LibHTTPC_malloc(sizeof(char *)); - else - request_buf->header_names = LibHTTPC_realloc(request_buf->header_names, sizeof(char *)); - - if (!request_buf->header_values) - request_buf->header_values = LibHTTPC_malloc(sizeof(char *)); + if (!request_buf->headers) + request_buf->headers = LibHTTPC_malloc(sizeof(struct LibHTTPC_Header)); else - request_buf->header_values = LibHTTPC_realloc(request_buf->header_values, sizeof(char *)); + request_buf->headers = LibHTTPC_realloc(request_buf->headers, sizeof(struct LibHTTPC_Header)); } if (i < request_buf->header_count) { - request_buf->header_names[i] = next; + request_buf->headers[i].name = next; next = strchr(next, ':'); *next++ = '\0'; - request_buf->header_values[i] = next; + request_buf->headers[i].value = next; } } request_buf->body = next + 2; @@ -80,10 +74,7 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b void LibHTTPC_Request_(struct LibHTTPC_Request *request) { if (request->header_selfalloc) - { - free(request->header_names); - free(request->header_values); - } + free(request->headers); if (request->selfalloc) free(request); } diff --git a/src/response.c b/src/response.c index cdcaef3..96e27cf 100644 --- a/src/response.c +++ b/src/response.c @@ -27,9 +27,9 @@ char *LibHTTPC_dumpResponse(struct LibHTTPC_Response *response, char *buf, size_ append("\r\n"); for (size_t i = 0; i < response->header_count; ++i) { - append(response->header_names[i]); + append(response->headers[i].name); append(": "); - append(response->header_values[i]); + append(response->headers[i].value); append("\r\n"); } append("\r\n"); |