feat(lander): full initial migration to lnm
	
		
			
	
		
	
	
		
			
				
	
				ci/woodpecker/push/build Pipeline was successful
				
					Details
				
			
		
	
				
					
				
			
				
	
				ci/woodpecker/push/build Pipeline was successful
				
					Details
				
			
		
	
							parent
							
								
									f3da5c78ef
								
							
						
					
					
						commit
						8ae59f1031
					
				| 
						 | 
				
			
			@ -46,24 +46,22 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn);
 | 
			
		|||
 | 
			
		||||
lnm_http_step_err lander_post_redirect(lnm_http_conn *conn);
 | 
			
		||||
 | 
			
		||||
bool lander_post_paste(event_loop_conn *conn);
 | 
			
		||||
lnm_http_step_err lander_post_paste(lnm_http_conn *conn);
 | 
			
		||||
 | 
			
		||||
bool lander_post_paste(event_loop_conn *conn);
 | 
			
		||||
 | 
			
		||||
bool lander_stream_body_to_entry(event_loop_conn *conn);
 | 
			
		||||
lnm_http_step_err lander_stream_body_to_entry(lnm_http_conn *conn);
 | 
			
		||||
 | 
			
		||||
bool lander_stream_body_to_client(event_loop_conn *conn);
 | 
			
		||||
 | 
			
		||||
lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn);
 | 
			
		||||
 | 
			
		||||
bool lander_remove_entry(event_loop_conn *conn);
 | 
			
		||||
lnm_http_step_err lander_remove_entry(lnm_http_conn *conn);
 | 
			
		||||
 | 
			
		||||
bool lander_post_file(event_loop_conn *conn);
 | 
			
		||||
lnm_http_step_err lander_post_file(lnm_http_conn *conn);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Store the requested header as an attribute, if it's present.
 | 
			
		||||
 */
 | 
			
		||||
void lander_header_to_attr(http_loop_ctx *ctx, const char *header,
 | 
			
		||||
void lander_header_to_attr(lnm_http_loop_ctx *ctx, const char *header,
 | 
			
		||||
                           lander_attr_type attr_type);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -84,23 +84,19 @@ void lander_ctx_reset(lander_ctx *ctx) {
 | 
			
		|||
 | 
			
		||||
void lander_ctx_free(lander_ctx *ctx) { free(ctx); }
 | 
			
		||||
 | 
			
		||||
void lander_header_to_attr(http_loop_ctx *ctx, const char *header_name,
 | 
			
		||||
void lander_header_to_attr(lnm_http_loop_ctx *ctx, const char *header_name,
 | 
			
		||||
                           lander_attr_type attr_type) {
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
  for (size_t i = 0; i < ctx->req.num_headers; i++) {
 | 
			
		||||
    const struct phr_header *header = &ctx->req.headers[i];
 | 
			
		||||
  const char *header_value;
 | 
			
		||||
  size_t header_value_len;
 | 
			
		||||
 | 
			
		||||
    if (strncmp(header->name, header_name, header->name_len) == 0) {
 | 
			
		||||
      if (header->value_len > 0) {
 | 
			
		||||
        lsm_str *value;
 | 
			
		||||
        lsm_str_init_copy_n(&value, (char *)header->value, header->value_len);
 | 
			
		||||
  if (lnm_http_req_header_get_s(&header_value, &header_value_len, &ctx->req,
 | 
			
		||||
                                header_name) == lnm_err_ok) {
 | 
			
		||||
    lsm_str *value;
 | 
			
		||||
    lsm_str_init_copy_n(&value, (char *)header_value, header_value_len);
 | 
			
		||||
 | 
			
		||||
        lsm_entry_attr_insert(c_ctx->entry, attr_type, value);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    lsm_entry_attr_insert(c_ctx->entry, attr_type, value);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -110,6 +106,8 @@ void lander_attr_to_header(lnm_http_loop_ctx *ctx, lander_attr_type attr_type,
 | 
			
		|||
  lsm_str *value;
 | 
			
		||||
 | 
			
		||||
  if (lsm_entry_attr_get(&value, c_ctx->entry, attr_type) == lsm_error_ok) {
 | 
			
		||||
    lnm_http_res_add_header_len(&ctx->res, header_type, (char *)lsm_str_ptr(value), lsm_str_len(value), false);
 | 
			
		||||
    lnm_http_res_add_header_len(&ctx->res, header_type,
 | 
			
		||||
                                (char *)lsm_str_ptr(value), lsm_str_len(value),
 | 
			
		||||
                                false);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,29 +1,30 @@
 | 
			
		|||
#include "lnm/loop.h"
 | 
			
		||||
 | 
			
		||||
#include "lander.h"
 | 
			
		||||
 | 
			
		||||
bool lander_remove_entry(event_loop_conn *conn) {
 | 
			
		||||
  http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
lnm_http_step_err lander_remove_entry(lnm_http_conn *conn) {
 | 
			
		||||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
  http_loop_gctx *gctx = ctx->g;
 | 
			
		||||
  lnm_http_loop_gctx *gctx = ctx->g;
 | 
			
		||||
  lander_gctx *c_gctx = gctx->c;
 | 
			
		||||
 | 
			
		||||
  const char *key_s = &ctx->req.path[ctx->req.regex_groups[1].rm_so];
 | 
			
		||||
  int key_len = ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so;
 | 
			
		||||
  const char *key_s = &ctx->req.path.s[ctx->req.path.groups[1].rm_so];
 | 
			
		||||
  int key_len = ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so;
 | 
			
		||||
 | 
			
		||||
  lsm_str *key;
 | 
			
		||||
  lsm_str_init_copy_n(&key, (char *)key_s, key_len);
 | 
			
		||||
 | 
			
		||||
  switch (lsm_store_open_write(&c_ctx->entry, c_gctx->store, key)) {
 | 
			
		||||
  case lsm_error_ok:
 | 
			
		||||
    lsm_entry_remove(c_ctx->entry);
 | 
			
		||||
    break;
 | 
			
		||||
  case lsm_error_not_found:
 | 
			
		||||
    ctx->res.status = http_not_found;
 | 
			
		||||
    return true;
 | 
			
		||||
    ctx->res.status = lnm_http_status_not_found;
 | 
			
		||||
    break;
 | 
			
		||||
  default:
 | 
			
		||||
    ctx->res.status = http_internal_server_error;
 | 
			
		||||
    return true;
 | 
			
		||||
    ctx->res.status = lnm_http_status_internal_server_error;
 | 
			
		||||
    break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  lsm_entry_remove(c_ctx->entry);
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,8 @@ lnm_http_step_err lander_get_index(lnm_http_conn *conn) {
 | 
			
		|||
 | 
			
		||||
  lnm_http_res_body_set_buf(&ctx->res, (char *)index_page,
 | 
			
		||||
                            sizeof(index_page) - 1, false);
 | 
			
		||||
  lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html", false);
 | 
			
		||||
  lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html",
 | 
			
		||||
                          false);
 | 
			
		||||
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -50,17 +51,20 @@ lnm_http_step_err lander_get_redirect(lnm_http_conn *conn) {
 | 
			
		|||
    return lnm_http_step_err_res;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  lnm_http_res_add_header_len(&ctx->res, lnm_http_header_location, (char *)lsm_str_ptr(url_attr_val), lsm_str_len(url_attr_val), false);
 | 
			
		||||
  lnm_http_res_add_header_len(&ctx->res, lnm_http_header_location,
 | 
			
		||||
                              (char *)lsm_str_ptr(url_attr_val),
 | 
			
		||||
                              lsm_str_len(url_attr_val), false);
 | 
			
		||||
 | 
			
		||||
  ctx->res.status = lnm_http_status_moved_permanently;
 | 
			
		||||
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
lnm_err lander_entry_data_streamer(uint64_t *written, char *buf, lnm_http_conn *conn,
 | 
			
		||||
    uint64_t offset, uint64_t len) {
 | 
			
		||||
lnm_err lander_entry_data_streamer(uint64_t *written, char *buf,
 | 
			
		||||
                                   lnm_http_conn *conn, uint64_t offset,
 | 
			
		||||
                                   uint64_t len) {
 | 
			
		||||
  // TODO respect offset variable
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -73,8 +77,10 @@ lnm_http_step_err lander_get_paste(lnm_http_conn *conn) {
 | 
			
		|||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
  lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer, lsm_entry_data_len(c_ctx->entry));
 | 
			
		||||
  lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/plain", false);
 | 
			
		||||
  lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer,
 | 
			
		||||
                           lsm_entry_data_len(c_ctx->entry));
 | 
			
		||||
  lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/plain",
 | 
			
		||||
                          false);
 | 
			
		||||
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -83,7 +89,8 @@ lnm_http_step_err lander_get_file(lnm_http_conn *conn) {
 | 
			
		|||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
  lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer, lsm_entry_data_len(c_ctx->entry));
 | 
			
		||||
  lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer,
 | 
			
		||||
                           lsm_entry_data_len(c_ctx->entry));
 | 
			
		||||
  lander_attr_to_header(ctx, lander_attr_type_content_type,
 | 
			
		||||
                        lnm_http_header_content_type);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -100,7 +107,8 @@ lnm_http_step_err lander_get_file(lnm_http_conn *conn) {
 | 
			
		|||
    strcpy(buf, "attachment");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  lnm_http_res_add_header(&ctx->res, lnm_http_header_content_disposition, buf, true);
 | 
			
		||||
  lnm_http_res_add_header(&ctx->res, lnm_http_header_content_disposition, buf,
 | 
			
		||||
                          true);
 | 
			
		||||
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,9 @@
 | 
			
		|||
#include "http/res.h"
 | 
			
		||||
#include "http/types.h"
 | 
			
		||||
#include "lander.h"
 | 
			
		||||
#include "lnm/loop.h"
 | 
			
		||||
#include "log.h"
 | 
			
		||||
#include "lsm/store.h"
 | 
			
		||||
#include "lnm/loop.h"
 | 
			
		||||
 | 
			
		||||
static void randomize_key(char *key, int len) {
 | 
			
		||||
  size_t charset_len = strlen(lander_key_charset);
 | 
			
		||||
| 
						 | 
				
			
			@ -93,29 +93,27 @@ lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn) {
 | 
			
		|||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool lander_post_paste(event_loop_conn *conn) {
 | 
			
		||||
  http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
lnm_http_step_err lander_post_paste(lnm_http_conn *conn) {
 | 
			
		||||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
  if (!lander_insert_entry(ctx)) {
 | 
			
		||||
    conn->state = event_loop_conn_state_res;
 | 
			
		||||
    return true;
 | 
			
		||||
    return lnm_http_step_err_res;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
 | 
			
		||||
                                lander_entry_type_paste);
 | 
			
		||||
  lander_header_to_attr(ctx, "X-Lander-Filename", lander_attr_type_file_name);
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool lander_post_file(event_loop_conn *conn) {
 | 
			
		||||
  http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
lnm_http_step_err lander_post_file(lnm_http_conn *conn) {
 | 
			
		||||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
  if (!lander_insert_entry(ctx)) {
 | 
			
		||||
    conn->state = event_loop_conn_state_res;
 | 
			
		||||
    return true;
 | 
			
		||||
    return lnm_http_step_err_res;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
 | 
			
		||||
| 
						 | 
				
			
			@ -124,5 +122,5 @@ bool lander_post_file(event_loop_conn *conn) {
 | 
			
		|||
                        lander_attr_type_content_type);
 | 
			
		||||
  lander_header_to_attr(ctx, "X-Lander-Filename", lander_attr_type_file_name);
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
  return lnm_http_step_err_done;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,22 +1,26 @@
 | 
			
		|||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "lander.h"
 | 
			
		||||
#include "lnm/http/loop.h"
 | 
			
		||||
#include "lnm/loop.h"
 | 
			
		||||
 | 
			
		||||
bool lander_stream_body_to_entry(event_loop_conn *conn) {
 | 
			
		||||
  http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
lnm_http_step_err lander_stream_body_to_entry(lnm_http_conn *conn) {
 | 
			
		||||
  lnm_http_loop_ctx *ctx = conn->ctx;
 | 
			
		||||
  lander_ctx *c_ctx = ctx->c;
 | 
			
		||||
 | 
			
		||||
  uint64_t to_append =
 | 
			
		||||
      MIN(conn->rbuf_size - conn->rbuf_read,
 | 
			
		||||
      MIN(conn->r.size - conn->r.read,
 | 
			
		||||
          ctx->req.body.expected_len - lsm_entry_data_len(c_ctx->entry));
 | 
			
		||||
 | 
			
		||||
  lsm_str *data;
 | 
			
		||||
  lsm_str_init_copy_n(&data, (char *)&conn->rbuf[conn->rbuf_read], to_append);
 | 
			
		||||
  lsm_str_init_copy_n(&data, (char *)&conn->r.buf[conn->r.read], to_append);
 | 
			
		||||
  lsm_entry_data_append(c_ctx->entry, data);
 | 
			
		||||
 | 
			
		||||
  conn->rbuf_read += to_append;
 | 
			
		||||
  conn->r.read += to_append;
 | 
			
		||||
 | 
			
		||||
  lsm_str_free(data);
 | 
			
		||||
 | 
			
		||||
  return lsm_entry_data_len(c_ctx->entry) == ctx->req.body.expected_len;
 | 
			
		||||
  return lsm_entry_data_len(c_ctx->entry) == ctx->req.body.expected_len
 | 
			
		||||
             ? lnm_http_step_err_done
 | 
			
		||||
             : lnm_http_step_err_io_needed;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										20
									
								
								src/main.c
								
								
								
								
							
							
						
						
									
										20
									
								
								src/main.c
								
								
								
								
							| 
						 | 
				
			
			@ -24,11 +24,29 @@ lnm_http_loop *loop_init(lander_gctx *gctx) {
 | 
			
		|||
  lnm_http_loop_route_add(hl, route);
 | 
			
		||||
 | 
			
		||||
  lnm_http_step_init(&step, lander_post_redirect);
 | 
			
		||||
  lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2, step);
 | 
			
		||||
  lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2,
 | 
			
		||||
                            step);
 | 
			
		||||
  lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf);
 | 
			
		||||
  lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr);
 | 
			
		||||
  lnm_http_loop_route_add(hl, route);
 | 
			
		||||
 | 
			
		||||
  lnm_http_step_init(&step, lander_post_paste);
 | 
			
		||||
  lnm_http_route_init_regex(&route, lnm_http_method_post, "^/p(l?)/([^/]*)$", 2,
 | 
			
		||||
                            step);
 | 
			
		||||
  lnm_http_step_append(&step, step, lander_stream_body_to_entry);
 | 
			
		||||
  lnm_http_loop_route_add(hl, route);
 | 
			
		||||
 | 
			
		||||
  lnm_http_step_init(&step, lander_post_file);
 | 
			
		||||
  lnm_http_route_init_regex(&route, lnm_http_method_post, "^/f(l?)/([^/]*)$", 2,
 | 
			
		||||
                            step);
 | 
			
		||||
  lnm_http_step_append(&step, step, lander_stream_body_to_entry);
 | 
			
		||||
  lnm_http_loop_route_add(hl, route);
 | 
			
		||||
 | 
			
		||||
  lnm_http_step_init(&step, lander_remove_entry);
 | 
			
		||||
  lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1,
 | 
			
		||||
                            step);
 | 
			
		||||
  lnm_http_loop_route_add(hl, route);
 | 
			
		||||
 | 
			
		||||
  return hl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue