示例#1
0
static PyObject *archonhttp_sendrequest(PyObject *self, PyObject *args,
                                        int method)
{
    Http_request req;
    Http_response resp;
    PyObject *resp_header_dict, *py_header_dict, *py_resp;
    int rc;
    const char *py_host;
    const char *py_path;
    const char *py_body;
    char *header_s;

    //Parse python arguments into a request
    if (!PyArg_ParseTuple(args, "sssO", &py_host, &py_path, &py_body,
                          &py_header_dict)) {
        return NULL;
    }
    memset(&req, 0, sizeof(Http_request));
    req.method = method;
    if ((header_s = build_headers_string(py_header_dict)) == NULL) {
        PyErr_SetString(PyExc_RuntimeError,
                        "Error building header string from dict");
        return NULL;
    }

    if (asprintf(&req.host, "%s", py_host) < 0 ||
        asprintf(&req.path, "%s", py_path) < 0 ||
        asprintf(&req.port, "%s", "80") < 0 ||
        asprintf(&req.headers, "%s", header_s) < 0 ||
        asprintf(&req.body, "%s", py_body) < 0) {
        PyErr_NoMemory();
        return NULL;
    }
    memset(&resp, 0, sizeof(Http_response));

    //Run
    if ((rc = SendRequest(&req, &resp)) != 0) {
        PyErr_SetString(PyExc_RuntimeError, "Error on send");
        return NULL;
    }

    //Return results to python
    resp_header_dict = build_headers_dict(&resp);
    py_resp = Py_BuildValue("sOssss", resp.body, resp_header_dict, resp.status,
                            resp.reason, resp.version,
                            resp.raw_response);
    FreeRequest(&req);
    return py_resp;
}
示例#2
0
文件: test.c 项目: wonghoifung/tips
int 
main() {
  struct reqcontext* ctx = create_reqcontext();

  for (size_t i = 0; i<data_len; ++i) {
    int ret = cparse(ctx, &(data[i]), 1, NULL);
    if (ret != 0) return ret;
  }

  printf("=============\n");
  printf("%s\n", data);
  printf("=============\n");
  char headers[8192] = {0};
  build_headers_string(&ctx->msg, headers, sizeof(headers), 0);
  printf("is headers equal: %d\n", strncmp(headers, data, strlen(headers))==0);

  delete_reqcontext(ctx);
}