Exemple #1
0
//  _tnetstring_loads:  parse tnetstring-format value from a string.
//
static PyObject*
_tnetstring_loads(PyObject* self, PyObject *args) 
{
  PyObject *string = NULL;
  PyObject *encoding = Py_None;
  PyObject *val = NULL;
  tns_ops *ops = &_tnetstring_ops_bytes;
  char *data;
  size_t len;

  if(!PyArg_UnpackTuple(args, "loads", 1, 2, &string, &encoding)) {
      return NULL;
  }
  if(!PyString_Check(string)) {
      PyErr_SetString(PyExc_TypeError, "arg must be a string");
      return NULL;
  }
  Py_INCREF(string);

  if(encoding == Py_None) {
      data = PyString_AS_STRING(string);
      len = PyString_GET_SIZE(string);
      val = tns_parse(ops, data, len, NULL);
  } else {
      if(!PyString_Check(encoding)) {
          PyErr_SetString(PyExc_TypeError, "encoding must be a string");
          goto error;
      }
      Py_INCREF(encoding);
      ops = _tnetstring_get_unicode_ops(encoding);
      if(ops == NULL) {
          Py_DECREF(encoding);
          goto error;
      }
      data = PyString_AS_STRING(string);
      len = PyString_GET_SIZE(string);
      val = tns_parse(ops, data, len, NULL);
      free(ops);
      Py_DECREF(encoding);
  }

  Py_DECREF(string);
  return val;

error:
  Py_DECREF(string);
  return NULL;
}
Exemple #2
0
static PyObject*
_tnetstring_pop(PyObject* self, PyObject *args) 
{
  PyObject *string, *val, *rest;
  char *data, *remain;
  size_t len;

  if(!PyArg_UnpackTuple(args, "pop", 1, 1, &string)) {
      return NULL;
  }
  if(!PyString_Check(string)) {
      PyErr_SetString(PyExc_TypeError, "arg must be a string");
      return NULL;
  }
  Py_INCREF(string);

  data = PyString_AS_STRING(string);
  len = PyString_GET_SIZE(string);
  val = tns_parse(data, len, &remain);
  Py_DECREF(string);
  if(val == NULL) {
      return NULL;
  }

  rest = PyString_FromStringAndSize(remain, len-(remain-data));
  return PyTuple_Pack(2, val, rest);
}
Exemple #3
0
static PyObject*
_tnetstring_loads(PyObject* self, PyObject *args) 
{
  PyObject *string, *val;
  char *data;
  size_t len;

  if(!PyArg_UnpackTuple(args, "loads", 1, 1, &string)) {
      return NULL;
  }
  if(!PyString_Check(string)) {
      PyErr_SetString(PyExc_TypeError, "arg must be a string");
      return NULL;
  }
  Py_INCREF(string);

  data = PyString_AS_STRING(string);
  len = PyString_GET_SIZE(string);
  val = tns_parse(data, len, NULL);
  Py_DECREF(string);
  if(val == NULL) {
      return NULL;
  }

  return val;
}
Exemple #4
0
static inline void handler_process_extended_request(int fd, Connection *conn, bstring payload)
{
    char *x;
    tns_value_t *data = NULL;
    darray_t *l = NULL;
    
    data = tns_parse(bdata(payload),blength(payload),&x);

    check((x-bdata(payload))==blength(payload), "Invalid extended response: extra data after tnetstring.");
    check(data->type==tns_tag_list, "Invalid extended response: not a list.");
    l = data->value.list;
    check(darray_end(l)==2, "Invalid extended response: odd number of elements in list.");
    tns_value_t *key=darray_get(l,0);
    check(key->type==tns_tag_string, "Invalid extended response: key is not a string");
    check(key->value.string != NULL,, "Invalid extended response: key is NULL");

    if(!bstrcmp(key->value.string, &XREQ_CTL)) {
        check (0 == handler_process_control_request(conn, data),
                "Control request processing returned non-zero: %s", bdata(key->value.string));
    } else {
        check (0 == dispatch_extended_request(conn, key->value.string, data),
                "Extended request dispatch returned non-zero: %s",bdata(key->value.string));
    }

    return;
error:
    tns_value_destroy(data);
    Register_disconnect(fd); // return ignored
    return;
}
Exemple #5
0
int Command_access_logs(Command *cmd)
{
    bstring log_filename = option(cmd, "log", "logs/access.log");
    check(log_filename, "Invalid log file given.");

    FILE *log_file = fopen(bdata(log_filename), "r");
    check(log_file != NULL, "Failed to open log file: %s", bdata(log_filename));

    int line_number = 0;
    bstring line;

    while ((line = bgets((bNgetc) fgetc, log_file, '\n')) != NULL) {
        line_number++;

        tns_value_t *log_item = tns_parse(bdata(line), blength(line), NULL);

        if (log_item == NULL ||
                tns_get_type(log_item) != tns_tag_list || 
                darray_end(log_item->value.list) < 9
                )
        {
            log_warn("Malformed log line: %d.", line_number);
            continue;
        }

        darray_t *entries = log_item->value.list;

        bstring hostname = darray_get_as(entries, 0, string);
        bstring remote_addr = darray_get_as(entries, 1, string);
        long remote_port = darray_get_as(entries, 2, number);
        long timestamp = darray_get_as(entries, 3, number);
        bstring request_method = darray_get_as(entries, 4, string);
        bstring request_path = darray_get_as(entries, 5, string);
        bstring version = darray_get_as(entries, 6, string);
        long status = darray_get_as(entries, 7, number);
        long size = darray_get_as(entries, 8, number);

        printf("[%ld] %s:%ld %s \"%s %s %s\" %ld %ld\n",
               timestamp,
               bdata(remote_addr),
               remote_port,
               bdata(hostname),
               bdata(request_method),
               bdata(request_path),
               bdata(version),
               status,
               size);

        tns_value_destroy(log_item);
    }

    return 0;

error:
    return -1;
}
Exemple #6
0
static PyObject*
_tnetstring_pop(PyObject* self, PyObject *args) 
{
  PyObject *string = NULL;
  PyObject *val = NULL;
  PyObject *rest = NULL;
  PyObject *result = NULL;
  PyObject *encoding = Py_None;
  tns_ops *ops = &_tnetstring_ops_bytes;
  char *data, *remain;
  size_t len;

  if(!PyArg_UnpackTuple(args, "pop", 1, 2, &string, &encoding)) {
      return NULL;
  }
  if(!PyString_Check(string)) {
      PyErr_SetString(PyExc_TypeError, "arg must be a string");
      return NULL;
  }
  if(encoding != Py_None) {
      if(!PyString_Check(encoding)) {
          PyErr_SetString(PyExc_TypeError, "encoding must be a string");
          return NULL;
      }
      Py_INCREF(encoding);
      ops = _tnetstring_get_unicode_ops(encoding);
      if(ops == NULL) {
          Py_DECREF(encoding);
          return NULL;
      }
  }
  Py_INCREF(string);

  data = PyString_AS_STRING(string);
  len = PyString_GET_SIZE(string);
  val = tns_parse(ops, data, len, &remain);
  Py_DECREF(string);
  if(ops != &_tnetstring_ops_bytes) {
      free(ops);
      Py_DECREF(encoding);
  }
  if(val == NULL) {
      return NULL;
  }

  rest = PyString_FromStringAndSize(remain, len-(remain-data));
  if(rest == NULL) {
      result = NULL;
  } else {
      result = PyTuple_Pack(2, val, rest);
      Py_DECREF(rest);
  }
  Py_DECREF(val);
  return result;
}
Exemple #7
0
char *test_Request_speeds()
{
    int i = 0;
    FILE *infile = fopen("tests/request_payloads.txt", "r");
    mu_assert(infile != NULL, "Failed to open the tests/request_payloads.txt test file.");
    struct bstrList *list = bstrListCreate();
    bstrListAlloc(list, 300);

    // load up all the ones we can
    for(i = 0; i < 300; i++, list->qty++) {
        bstring sender = bgets((bNgetc)fgetc, infile, ' ');
        bstring conn_id = bgets((bNgetc)fgetc, infile, ' ');
        bstring path = bgets((bNgetc)fgetc, infile, ' ');
        list->entry[i] = bgets((bNgetc)fgetc, infile, '\n');

        // stop if we didn't read anything
        if(!(sender && conn_id && path)) break;

        bdestroy(sender);
        bdestroy(conn_id);
        bdestroy(path);
    }

    fclose(infile);


    // now rip through and parse them for speed test
    int j = 0;
    for(j = 0; j < 200; j++) {
        for(i = 0; i < list->qty; i++) {
            tns_value_t *val = tns_parse(bdata(list->entry[i]), blength(list->entry[i]), NULL);
            mu_assert(val->type == tns_tag_string || val->type == tns_tag_dict, "Got an invalid data chunk from file.");

            size_t len = 0;
            char *payload = tns_render(val, &len);
            mu_assert(len > 0, "Failed to render the payload.");

            free(payload);
            tns_value_destroy(val);
        }
    }

    bstrListDestroy(list);
    return NULL;
}