aboutsummaryrefslogtreecommitdiff
path: root/src/molerat/request.c
blob: 1d59a09a259e163d1c12964aa4cbb3c2928a1aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>

#include "request.h"
#include "url.h"

char *get_request_kind(enum RequestKind kind) {
  switch (kind) {
  case GET:
    return "get";
  case PUT:
    return "put";
  case DEL:
    return "del";
  }
}

char *request_to_string(struct request *req) {
  int len = sizeof(struct request) +
            6; // +1 for null terminator +5 for request whitespace

  char *buf = malloc(len);

  char *kind = get_request_kind(req->kind);
  char *host = req->url.host != NULL ? req->url.host : "";
  char *path = req->url.path != NULL ? req->url.path : "";
  char *query = req->url.query != NULL ? req->url.query : "";
  char *fragment = req->url.fragment != NULL ? req->url.fragment : "";

  snprintf(buf, len, "%s %s%s%s%s\r\n\r\n", kind, host, path, query, fragment);

  return buf;
}