Skip to content

Commit

Permalink
Add convenience function to convert strbuf to lwan_value
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Aug 17, 2024
1 parent 33f31e4 commit 4b47775
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/lib/lwan-mod-fastcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ try_initiating_chunked_response(struct lwan_request *request)
char *header_start[N_HEADER_START];
char *next_request;
enum lwan_http_status status_code = HTTP_OK;
struct lwan_value buffer = {
.value = lwan_strbuf_get_buffer(response->buffer),
.len = lwan_strbuf_get_length(response->buffer),
};
struct lwan_value buffer = lwan_strbuf_to_value(response->buffer);

assert(!(request->flags &
(RESPONSE_CHUNKED_ENCODING | RESPONSE_SENT_HEADERS)));
Expand Down
5 changes: 1 addition & 4 deletions src/lib/lwan-mod-serve-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,7 @@ static bool dirlist_init(struct file_cache_entry *ce,

ce->mime_type = "text/html";

struct lwan_value rendered = {
.value = lwan_strbuf_get_buffer(&dd->rendered),
.len = lwan_strbuf_get_length(&dd->rendered),
};
struct lwan_value rendered = lwan_strbuf_to_value(&dd->rendered);
deflate_value(&rendered, &dd->deflated);
#if defined(LWAN_HAVE_BROTLI)
brotli_value(&rendered, &dd->brotli, &dd->deflated);
Expand Down
13 changes: 13 additions & 0 deletions src/lib/lwan-strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>

#include "lwan-private.h"
Expand Down Expand Up @@ -438,3 +439,15 @@ struct lwan_strbuf *lwan_strbuf_new_from_file(const char *path)
free(strbuf);
return NULL;
}

struct lwan_value lwan_strbuf_to_value(const struct lwan_strbuf *s)
{
return (struct lwan_value){.value = lwan_strbuf_get_buffer(s),
.len = lwan_strbuf_get_length(s)};
}

struct iovec lwan_strbuf_to_iovec(const struct lwan_strbuf *s)
{
return (struct iovec){.iov_base = lwan_strbuf_get_buffer(s),
.iov_len = lwan_strbuf_get_length(s)};
}
8 changes: 2 additions & 6 deletions src/lib/lwan-strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,5 @@ static inline char *lwan_strbuf_get_buffer(const struct lwan_strbuf *s)
bool lwan_strbuf_init_from_file(struct lwan_strbuf *s, const char *path);
struct lwan_strbuf *lwan_strbuf_new_from_file(const char *path);

static inline struct iovec
lwan_strbuf_to_iovec(const struct lwan_strbuf *s)
{
return (struct iovec){.iov_base = lwan_strbuf_get_buffer(s),
.iov_len = lwan_strbuf_get_length(s)};
}
struct iovec lwan_strbuf_to_iovec(const struct lwan_strbuf *s);
struct lwan_value lwan_strbuf_to_value(const struct lwan_strbuf *s);
3 changes: 1 addition & 2 deletions src/lib/lwan.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ static void build_response_headers(struct lwan *l,

lwan_strbuf_append_strz(&strbuf, "\r\n\r\n");

l->headers = (struct lwan_value){.value = lwan_strbuf_get_buffer(&strbuf),
.len = lwan_strbuf_get_length(&strbuf)};
l->headers = lwan_strbuf_to_value(&strbuf);
}

static void parse_global_headers(struct config *c,
Expand Down

0 comments on commit 4b47775

Please sign in to comment.