Beispiel #1
0
void guava_router_static_free(guava_router_static_t *router) {
  if (!router) {
    return;
  }

  if (router->route.mount_point) {
    guava_string_free(router->route.mount_point);
  }

  if (router->route.package) {
    guava_string_free(router->route.package);
  }

  if (router->route.session_store) {
    guava_session_store_free(router->route.session_store);
  }

  if (router->route.routes) {
    Py_DECREF(router->route.routes);
  }

  if (router->directory) {
    guava_string_free(router->directory);
  }

  guava_free(router);
}
Beispiel #2
0
void guava_request_free(guava_request_t *req) {
  if (req->url) {
    guava_string_free(req->url);
    req->url = NULL;
  }

  if (req->path) {
    guava_string_free(req->path);
    req->path = NULL;
  }

  if (req->host) {
    guava_string_free(req->host);
    req->host = NULL;
  }

  if (req->body) {
    guava_string_free(req->body);
    req->body = NULL;
  }

  if (req->headers) {
    Py_DECREF(req->headers);
    req->headers = NULL;
  }

  if (req->GET) {
    Py_DECREF(req->GET);
    req->GET = NULL;
  }

  if (req->POST) {
    Py_DECREF(req->POST);
    req->POST = NULL;
  }

  if (req->COOKIES) {
    Py_DECREF(req->COOKIES);
    req->COOKIES = NULL;
  }

  guava_free(req);
}
Beispiel #3
0
void guava_router_static_set_directory(guava_router_static_t *router, const char *directory) {
  if (!router || !directory) {
    return;
  }

  if (router->directory) {
    guava_string_free(router->directory);
  }

  router->directory = guava_string_new(directory);
}
Beispiel #4
0
int guava_request_on_body(http_parser *parser, const char *buf, size_t len) {
  guava_conn_t *conn = (guava_conn_t *)parser->data;
  Request *request = (Request *)conn->request;
  if (len) {
    request->req->body = guava_string_append_raw_size(request->req->body, buf, len);

    PyObject *content_type = PyDict_GetItemString(request->req->headers, "Content-Type");
    if (content_type) {
      char *form_data = request->req->body;
      char **p = &form_data;
      if (strcmp(PyString_AsString(content_type), "application/x-www-form-urlencoded") == 0) {
        guava_string_t name = NULL;
        guava_string_t value = NULL;

        while (guava_request_parse_form_data(p, &name, &value)) {
          PyObject *s = PyString_FromString(value);
          PyDict_SetItemString(request->req->POST, name, s);
          Py_DECREF(s);

          name = NULL;
          value = NULL;
        }

        if (name) {
          guava_string_free(name);
        }
        if (value) {
          guava_string_free(value);
        }

      }
    }
  }

  return 0;
}
Beispiel #5
0
int guava_request_on_header_value(http_parser *parser, const char *buf, size_t len) {
  guava_conn_t *conn = (guava_conn_t *)parser->data;
  Request *request = (Request *)conn->request;

  if (conn->auxiliary_last_was_header && conn->auxiliary_current_header) {
    PyObject *v = PyString_FromStringAndSize(buf, len);
    PyDict_SetItemString(request->req->headers, conn->auxiliary_current_header, v);
    Py_DECREF(v);
    guava_string_free(conn->auxiliary_current_header);
    conn->auxiliary_current_header = NULL;
  }

  conn->auxiliary_last_was_header = 0;

  return 0;
}
Beispiel #6
0
void guava_request_extract_from_url(guava_request_t *req) {
  if (!req->url) {
    return;
  }

  char *ptr = strchr(req->url, '?');

  if (!ptr) {
    req->path = guava_string_new(req->url);
  } else {
    req->path = guava_string_new_size(req->url, ptr - (char *)req->url);
    ptr += 1;
    if (ptr != NULL) {
      char *and_ptr = strchr(ptr, '&');
      char *equal_ptr = NULL;

      while (ptr) {
        if (!and_ptr) {
          equal_ptr = strchr(ptr, '=');
          if (equal_ptr) {
            PyObject *key = PyString_FromStringAndSize(ptr, equal_ptr-ptr);
            PyObject *value = PyString_FromString(equal_ptr+1);
            PyDict_SetItem(req->GET, key, value);
          }
          break;
        } else {
          guava_string_t v = guava_string_new_size(ptr, and_ptr-ptr);
          equal_ptr = strchr(ptr, '=');
          if (equal_ptr) {
            PyObject *key = PyString_FromStringAndSize(ptr, equal_ptr-ptr);
            PyObject *value = PyString_FromStringAndSize(equal_ptr+1, and_ptr-equal_ptr-1);
            PyDict_SetItem(req->GET, key, value);
          }
          guava_string_free(v);
          ptr = and_ptr + 1;
          if (!ptr) {
            break;
          }
          and_ptr = strchr(ptr, '&');
        }
      }
    }
  }
}