From c5ddf04afa78dc879827fce5cf50de03b35bf110 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Sun, 15 Dec 2024 15:10:03 +0300 Subject: Implement LibHTTPC_readRequest Not complete, though --- TODO.7 | 16 ++++----- include/libhttpc.h | 7 +++- man/libhttpc_loadrequest.3 | 85 +++++++++++++++++++++++++++++++++++++++++++--- src/request.c | 29 ++++++++++++++++ 4 files changed, 124 insertions(+), 13 deletions(-) diff --git a/TODO.7 b/TODO.7 index ad17be3..5bac39c 100644 --- a/TODO.7 +++ b/TODO.7 @@ -11,19 +11,19 @@ with this library .Sh DESCRIPTION .Bl -bullet .It -Implement -.Fn LibHTTPC_readRequest . -It should be able to -parse requests -from -.Xr socket 2 -directly. -.It Implement handler. It should be a function that calls other ones depending on the .Xr uri 7 . +.It +Implement behavior +for +.Xr libhttpc_readrequest 3 +when +.Fa buf +is +.Dv NULL . .El . .Sh AUTHORS diff --git a/include/libhttpc.h b/include/libhttpc.h index 241fac0..35f1cca 100644 --- a/include/libhttpc.h +++ b/include/libhttpc.h @@ -280,7 +280,12 @@ int LibHTTPC_Request_(struct LibHTTPC_Request *request); #ifdef LibHTTPC_SOCK /** - * Not implemented yet + * Read request from sockfd + * @param sockfd From where to read request + * @param[out] request_buf Where to save request. Can be NULL, then library will allocate it itself + * @param[out] buf Where to save received data, can't be NULL. + * @param buf_len Length of buffer, must be greater than 0 + * @return Pointer to request on success, NULL otherwise */ struct LibHTTPC_Request *LibHTTPC_readRequest( int sockfd, diff --git a/man/libhttpc_loadrequest.3 b/man/libhttpc_loadrequest.3 index d926953..b25e09d 100644 --- a/man/libhttpc_loadrequest.3 +++ b/man/libhttpc_loadrequest.3 @@ -1,9 +1,10 @@ -.Dd December 14, 2024 +.Dd December 15, 2024 .Dt LIBHTTPC_LOADREQUEST 3 .Os . .Sh NAME -.Nm LibHTTPC_loadRequest +.Nm LibHTTPC_loadRequest , +.Nm LibHTTPC_readRequest .Nd parse HTTP request . .Sh SYNOPSIS @@ -13,6 +14,13 @@ .Fa "struct LibHTTPC_Request *request_buf" .Fa "char *buf" .Fc +.Ft "struct LibHTTPC_Request *" +.Fo LibHTTPC_readRequest +.Fa "int sockfd" +.Fa "struct LibHTTPC_Request *request_buf" +.Fa "char *buf" +.Fa "size_t buf_len" +.Fc . .Sh DESCRIPTION .Fn LibHTTPC_loadRequest @@ -20,12 +28,27 @@ parses .Fa buf and fills .Fa request_buf . +. +.Pp +.Fn LibHTTPC_readRequest +parses +.Xr libhttpc_request 3 +from +.Xr socket 2 +directly. +.Fa buf +can't be NULL. +Behavior +for this should be +implemented later, +though. +. +.Pp If .Fa request_buf -argument is .Dv NULL , -then functon will +then function will allocate buffer itself. . @@ -69,6 +92,55 @@ section of for more information. .El . +.Pp +If +.Fn LibHTTPC_readRequest +returned +.Dv NULL , +it could mean +either: +.Bl -bullet +.It +If +.Xr errno 3 +is 0 +(Success), +then either: +.Bl -bullet +.It +Either +.Fa buf +is +.Dv NULL +or +.Fa buf_len +is 0 +.It +Read text about +return values of +.Fn LibHTTPC_loadRequest +.El +.It +If +.Xr errno 3 +is not 0, +it means that either: +.Bl -bullet +.It +.Xr recv 2 +has failed. +Read +.Ql ERRORS +section of +.Xr recv 2 +for more information. +.It +Read text about +return values of +.Fn LibHTTPC_loadRequest +.El +.El +. .Sh SEE ALSO .Xr libhttpc 3 , .Xr libhttpc-alloc 3 , @@ -88,3 +160,8 @@ It sets on the end of every string in .Fa request_buf . +Note that +.Fn LibHTTPC_readRequest +uses +.Fn LibHTTPC_loadRequest +under the hood. diff --git a/src/request.c b/src/request.c index a0e1358..93d2421 100644 --- a/src/request.c +++ b/src/request.c @@ -4,6 +4,9 @@ #include #include +#include +#include + struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_buf, char *buf) { @@ -93,6 +96,32 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b return request_buf; } +#ifdef LibHTTPC_SOCK +struct LibHTTPC_Request *LibHTTPC_readRequest( + int sockfd, + struct LibHTTPC_Request *request_buf, + char *buf, size_t buf_len +) +{ + if (!buf) + { + /* TODO: Implement behavior when buf == NULL */ + return NULL; + } else + { + if (!buf_len) + return NULL; + + ssize_t received = recv(sockfd, buf, buf_len, 0); + if (received < 0) + return NULL; + buf[buf_len - 1] = '\0'; + + return LibHTTPC_loadRequest(request_buf, buf); + } +} +#endif + int LibHTTPC_Request_(struct LibHTTPC_Request *request) { if (!LibHTTPC_free) -- cgit 1.4.1