<commit_before>/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#define SERVER_NAME "localhost"
#define SERVER_PORT 4434

#if 0

  ./client -peer HOSTNAME to use non localhost peer

Basic client connects to server, does a handshake and and waits 1 seconds.. then..

  -streamtest1 will send 3 messages to the server including the keywords PREAMBLE and FIN
               the server will reply with 1 message and close the bidi stream
               after recpt of stream-close client will wait 2 seconds

  -send-close option will send a close before exiting

  -ignorePKI option will allow handshake with untrusted cert. (localhost always implies ignorePKI)

  -get PATH will get the uri. e.g. -get /main.jpg. You can have N of these
      
About Certificate Verifcation::
The sample/nss-config directory is a sample that can be passed
to mozquic_nss_config(). It contains a NSS database with a cert
and key for foo.example.com that is signed by a CA defined by CA.cert.der.
#endif

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include "../MozQuic.h"

static uint8_t recvFin = 0;
static int _argc;
static char **_argv;
static int _getCount = 0;

static int connEventCB(void *closure, uint32_t event, void *param)
{
  if (event == MOZQUIC_EVENT_CONNECTED) {
    int j;
    for (j=0; j < _argc - 1; j++) {
      if (!strcasecmp(_argv[j], "-get")) {
        mozquic_stream_t *stream;
        int code = mozquic_start_new_stream(&stream, param, "GET ", 4, 0);
        assert(code == MOZQUIC_OK);
        code = mozquic_send(stream, _argv[j+1], strlen(_argv[j+1]), 0);
        assert(code == MOZQUIC_OK);
        code = mozquic_send(stream, "\r\n", 2, 0);
        assert(code == MOZQUIC_OK);
        _getCount++;
      }
    }
  }
  
  if (event == MOZQUIC_EVENT_NEW_STREAM_DATA) {
    mozquic_stream_t *stream = param;
    char buf[1000];
    uint32_t amt = 0;
    int fin = 0;
    int line = 0;
    do {
      uint32_t code = mozquic_recv(stream, buf, 100, &amt, &fin);
      if (code != MOZQUIC_OK) {
        fprintf(stderr,"recv stream error %d\n", code);
        return MOZQUIC_OK;
      } else if (amt > 0) {
        if (!line) {
          fprintf(stderr,"Data:\n");
        }
        line++;
        fprintf(stderr,"[%d] fin=%d\n", amt, fin);
        if (fin) {
          recvFin = 1;
          if (_getCount) {
            if (!--_getCount) {
              _getCount = -1;
            }
          }
        }
      }
    } while (amt > 0);

    mozquic_end_stream(stream);
    return MOZQUIC_OK;
  } else if (event == MOZQUIC_EVENT_IO) {
  } else if (event == MOZQUIC_EVENT_CLOSE_CONNECTION ||
             event == MOZQUIC_EVENT_ERROR) {
    mozquic_destroy_connection(param);
    exit(event == MOZQUIC_EVENT_ERROR ? 2 : 0);
  } else {
    fprintf(stderr,"unhandled event %X\n", event);
  }<commit_msg>get cmdline can now save resources to disk<commit_after>/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#define SERVER_NAME "localhost"
#define SERVER_PORT 4434

#if 0

  ./client -peer HOSTNAME to use non localhost peer

Basic client connects to server, does a handshake and and waits 1 seconds.. then..

  -streamtest1 will send 3 messages to the server including the keywords PREAMBLE and FIN
               the server will reply with 1 message and close the bidi stream
               after recpt of stream-close client will wait 2 seconds

  -send-close option will send a close before exiting

  -ignorePKI option will allow handshake with untrusted cert. (localhost always implies ignorePKI)

  -get PATH will get the uri. e.g. -get /main.jpg. You can have N of these
      
About Certificate Verifcation::
The sample/nss-config directory is a sample that can be passed
to mozquic_nss_config(). It contains a NSS database with a cert
and key for foo.example.com that is signed by a CA defined by CA.cert.der.
#endif

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include "../MozQuic.h"

static uint8_t recvFin = 0;
static int _argc;
static char **_argv;
static int _getCount = 0;
static FILE *fd[256];

static int connEventCB(void *closure, uint32_t event, void *param)
{
  if (event == MOZQUIC_EVENT_CONNECTED) {
    int j;
    for (j=0; j < _argc - 1; j++) {
      if (!strcasecmp(_argv[j], "-get")) {
        mozquic_stream_t *stream;
        int code = mozquic_start_new_stream(&stream, param, "GET ", 4, 0);
        assert(code == MOZQUIC_OK);
        code = mozquic_send(stream, _argv[j+1], strlen(_argv[j+1]), 0);
        assert(code == MOZQUIC_OK);
        code = mozquic_send(stream, "\r\n", 2, 0);
        assert(code == MOZQUIC_OK);
        _getCount++;
        char pathname[1024];
        snprintf(pathname, 1024, "/tmp/get-%d", mozquic_get_streamid(stream));
        fd[mozquic_get_streamid(stream)] = fopen(pathname, "w");
        assert(fd[mozquic_get_streamid(stream)] != NULL);
      }
    }
  }
  
  if (event == MOZQUIC_EVENT_NEW_STREAM_DATA) {
    mozquic_stream_t *stream = param;
    char buf[1000];
    uint32_t amt = 0;
    int fin = 0;
    do {
      uint32_t code = mozquic_recv(stream, buf, 1000, &amt, &fin);
      if (code != MOZQUIC_OK) {
        fprintf(stderr,"recv stream error %d\n", code);
        return MOZQUIC_OK;
      } else if (amt > 0) {
        fprintf(stderr,"Data: stream %d %d fin=%d\n",
                mozquic_get_streamid(stream), amt, fin);
        for (size_t j=0; j < amt; ) {
          size_t rv = fwrite(buf + j, 1, amt - j, fd[mozquic_get_streamid(stream)]);
          assert(rv > 0);
          j += rv;
        }
        
        if (fin) {
          if (fd[mozquic_get_streamid(stream)]) {
            fclose (fd[mozquic_get_streamid(stream)]);
            fd[mozquic_get_streamid(stream)] = NULL;
          }
          recvFin = 1;
          if (_getCount) {
            if (!--_getCount) {
              _getCount = -1;
            }
          }
        }
      }
    } while (amt > 0);

    mozquic_end_stream(stream);
    return MOZQUIC_OK;
  } else if (event == MOZQUIC_EVENT_IO) {
  } else if (event == MOZQUIC_EVENT_CLOSE_CONNECTION ||
             event == MOZQUIC_EVENT_ERROR) {
    mozquic_destroy_connection(param);
    exit(event == MOZQUIC_EVENT_ERROR ? 2 : 0);
  } else {
    fprintf(stderr,"unhandled event %X\n", event);
  }
{'fraction_non_alphanumeric': 0.11767304860088365, 'fraction_numerical': 0.01678939617083947, 'mean_word_length': 2.579862941486558, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6134216', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2316, 'n_words': 715}