summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx.manpages@gmail.com>2022-08-31 00:19:23 +0200
committerAlejandro Colomar <alx.manpages@gmail.com>2022-09-02 00:37:42 +0200
commit245bc9c437e8e66a012733eedf37101a47ee9ca7 (patch)
tree1c2283c859ba8b929c1dcc2c193e12bb54d3e832
parent940d5cb7ce9537e997ad2dee863997c94e65b2d9 (diff)
Try to connect to another backend app
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
-rw-r--r--src/nxt_unit_app_test.c57
1 files changed, 54 insertions, 3 deletions
diff --git a/src/nxt_unit_app_test.c b/src/nxt_unit_app_test.c
index 1aeca62..51df731 100644
--- a/src/nxt_unit_app_test.c
+++ b/src/nxt_unit_app_test.c
@@ -5,10 +5,15 @@
#include <nxt_unit.h>
#include <nxt_unit_request.h>
+#include <err.h>
//#include <nxt_clang.h>
#include <pthread.h>
#include <string.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
#define nxt_expect(c, x) __builtin_expect((long) (x), (c))
@@ -157,11 +162,57 @@ greeting_app_request_handler(nxt_unit_request_info_t *req)
nxt_unit_field_t *f;
nxt_unit_request_t *r;
+ int sfd;
+ ssize_t rd, wr;
+ struct sockaddr_un sun;
+ char buff[14];
+
+ rc = NXT_UNIT_ERROR;
+
+ warnx("ALX: socket()");
+ sfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (nxt_slow_path(sfd == -1)) {
+ warn("ALX: fail");
+ goto fail;
+ }
+
+ sun.sun_family = AF_UNIX;
+ warnx("ALX: memset()");
+ memcpy(sun.sun_path, "\0back", 6);
+ warnx("ALX: connect()");
+ if (nxt_slow_path(connect(sfd, (struct sockaddr *)&sun,
+ offsetof(struct sockaddr_un, sun_path) + 5)))
+ {
+ warn("ALX: fail");
+ goto fail;
+ }
+ char get[] = "GET / HTTP/1.0\n\n";
+ fprintf(stderr, "ALX: write(): ");
+ wr = write(sfd, get, sizeof(get));
+ fprintf(stderr, "%zd(%zu)\n", wr, sizeof(get));
+ if (nxt_slow_path(wr != sizeof(get))) {
+ warn("ALX: fail");
+ goto fail;
+ }
+ fprintf(stderr, "ALX: read(): ");
+ rd = read(sfd, buff, 12);
+ fprintf(stderr, "%zd\n", rd);
+ if (nxt_slow_path(rd < 12)) {
+ warn("ALX: fail");
+ goto fail;
+ }
+ warnx("ALX: close()");
+ if (nxt_slow_path(close(sfd) == -1)) {
+ warn("ALX: fail");
+ goto fail;
+ }
+ warn("ALX:");
+
rc = nxt_unit_response_init(req, 200 /* Status code. */,
1 /* Number of response headers. */,
nxt_length(CONTENT_TYPE)
+ nxt_length(TEXT_PLAIN)
- + nxt_length(HELLO_WORLD));
+ + nxt_length(buff));
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
goto fail;
}
@@ -173,8 +224,8 @@ greeting_app_request_handler(nxt_unit_request_info_t *req)
goto fail;
}
- rc = nxt_unit_response_add_content(req, HELLO_WORLD,
- nxt_length(HELLO_WORLD));
+ rc = nxt_unit_response_add_content(req, buff,
+ nxt_length(buff));
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
goto fail;
}