Ejemplo n.º 1
0
int pkproto_test_parser(struct pk_parser* p, int *callback_called)
{
  char* testchunk = "SID: 1\r\neOf: r\r\nNOOP: !\r\n\r\n54321";
  char buffer[100], framehead[10];
  int length;
  int bytes_left = p->buffer_bytes_left;

  assert(pk_parser_parse(p, 8, "z\r\n12345") == ERR_PARSE_BAD_FRAME);
  pk_parser_reset(p);

  assert(pk_parser_parse(p, 8, "5\r\n54321") == ERR_PARSE_BAD_CHUNK);
  pk_parser_reset(p);

  length = strlen(testchunk);
  length += sprintf(buffer, "%x\r\n", length);
  strcpy(framehead, buffer);
  strcat(buffer, testchunk);
  strcat(buffer, framehead);
  strcat(buffer, testchunk);

  assert(2*length == (int) strlen(buffer));
  assert(pk_parser_parse(p, 2*length-10, buffer) == 2*length-10);
  assert(pk_parser_parse(p, 10, buffer+2*length-10) == 10);

  /* After parsing, the callback should have been called twice, all
   * buffer space released for use and the chunk been reset. */
  assert(*callback_called == 2);
  assert(p->buffer_bytes_left == bytes_left);
  assert(p->chunk->data == NULL);

  return 1;
}
Ejemplo n.º 2
0
int main(int argc, char **argv) {
  char pbuffer[64000];
  struct pk_conn pkc;
  struct pk_parser* pkp;
  struct pk_pagekite kite;
  struct pk_kite_request kite_r;
  struct pk_kite_request* kite_rp;
  SSL_CTX* ctx;

  if (argc < 3) {
    usage();
    exit(1);
  }

  pks_global_init(PK_LOG_ALL);
  pk_state.log_file = NULL;
  PKS_SSL_INIT(ctx);

  kite_r.kite = &kite;
  strcpy(kite.protocol, "http");
  strncpyz(kite.public_domain, argv[1], PK_DOMAIN_LENGTH);
  kite.public_port = 0;
  strncpyz(kite.auth_secret, argv[2], PK_SECRET_LENGTH);

  kite_r.bsalt[0] = '\0';
  kite_r.fsalt[0] = '\0';
  kite_rp = &kite_r;

  srand(time(0) ^ getpid());
  if (0 > pk_connect(&pkc, argv[1], 443, 1, &kite_r, NULL, ctx)) {
    pk_perror(argv[1]);
    usage();
    return 1;
  }

  pkp = pk_parser_init(sizeof(pbuffer), pbuffer, &handle_request, &pkc);
  if (NULL == pkp) {
    pk_perror(argv[1]);
    usage();
    return 1;
  }

  fprintf(stderr, "*** Connected! ***\n");
  while (pkc_wait(&pkc, -1)) {
    pkc_read(&pkc);
    pk_parser_parse(pkp, pkc.in_buffer_pos, (char *) pkc.in_buffer);
    pkc.in_buffer_pos = 0;
  }
  pkc_reset_conn(&pkc, 0);

  return 0;
}