<commit_before>/* This file is part of the YAZ toolkit.
 * Copyright (C) 1995-2011 Index Data
 * See the file LICENSE for details.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <yaz/url.h>
#include <yaz/options.h>
#include <yaz/log.h>

static void usage(void)
{
    printf("yaz-icu [options] url ..\n");
    printf(" -H name=value       HTTP header\n");
    printf(" -p file             POST content of file\n");
    printf(" -u user/password    Basic HTTP auth\n");
    printf(" -x proxy            HTTP proxy\n");
    exit(1);
}

static char *get_file(const char *fname, size_t *len)
{
    char *buf = 0;
    FILE *inf = fopen(fname, "rb");
    if (!inf)
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
        exit(1);
    }
    if (fseek(inf, 0L, SEEK_END))
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
        exit(1);
    }
    *len = ftell(inf);
    buf = xmalloc(*len);
    fseek(inf, 0L, SEEK_SET);
    fread(buf, 1, *len, inf);
    fclose(inf);
    return buf;
}

int main(int argc, char **argv)
{
    int ret;
    char *arg;
    yaz_url_t p = yaz_url_create();
    char *post_buf = 0;
    size_t post_len = 0;
    const char *method = "GET";
    Z_HTTP_Response *http_response;
    Z_HTTP_Header *http_headers = 0;
    ODR odr = odr_createmem(ODR_ENCODE);
    int exit_code = 0;

    while ((ret = options("hH:p:u:x:", argv, argc, &arg))
           != YAZ_OPTIONS_EOF)
    {
        switch (ret)
        {
        case 'h':
            usage();
            break;
        case 'H':
            if (!strchr(arg, '='))
            {
                yaz_log(YLOG_FATAL, "bad header option (missing =): %s\n", arg);
                exit_code = 1;
            }
            else
            {
                char *cp = strchr(arg, '=');
                char *name = odr_malloc(odr, 1 + cp - arg);
                char *value = cp + 1;
                memcpy(name, arg, cp - arg);
                name[cp - arg] = '\0';
                z_HTTP_header_add(odr, &http_headers, name, value);
            }
            break;
        case 'p':
            xfree(post_buf);
            post_buf = get_file(arg, &post_len);
            method = "POST";
            break;
        case 'u':
            if (strchr(arg, '/'))
            {
                char *cp = strchr(arg, '/');
                char *user = odr_malloc(odr, 1 + cp - arg);
                char *password = <PASSWORD>;
                memcpy(user, arg, cp - arg);
                user[cp - arg] = '\0';
                z_HTTP_header_add_basic_auth(odr, &http_headers, user,
                                             password);
            }
            else
                z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
            break;
        case 'x':
            yaz_url_set_proxy(p, arg);
            break;
        case 0:
            http_response = yaz_url_exec(p, arg, method, http_headers,
                                         post_buf, post_len);
            if (!http_response)
                exit_code = 1;
            else
            {
                fwrite(http_response->content_buf, 1,
                       http_response->content_len, stdout);
            }
            break;
        default:
            usage();
        }
    }
    yaz_url_destroy(p);
    odr_destroy(odr);
    exit(exit_code);
}

/*<commit_msg>yaz-url: display usage if no ULRs are given<commit_after>/* This file is part of the YAZ toolkit.
 * Copyright (C) 1995-2011 Index Data
 * See the file LICENSE for details.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <yaz/url.h>
#include <yaz/options.h>
#include <yaz/log.h>

static void usage(void)
{
    printf("yaz-icu [options] url ..\n");
    printf(" -H name=value       HTTP header\n");
    printf(" -p file             POST content of file\n");
    printf(" -u user/password    Basic HTTP auth\n");
    printf(" -x proxy            HTTP proxy\n");
    exit(1);
}

static char *get_file(const char *fname, size_t *len)
{
    char *buf = 0;
    FILE *inf = fopen(fname, "rb");
    if (!inf)
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "Could not open %s", fname);
        exit(1);
    }
    if (fseek(inf, 0L, SEEK_END))
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "fseek of %s failed", fname);
        exit(1);
    }
    *len = ftell(inf);
    buf = xmalloc(*len);
    fseek(inf, 0L, SEEK_SET);
    fread(buf, 1, *len, inf);
    fclose(inf);
    return buf;
}

int main(int argc, char **argv)
{
    int ret;
    char *arg;
    yaz_url_t p = yaz_url_create();
    char *post_buf = 0;
    size_t post_len = 0;
    const char *method = "GET";
    Z_HTTP_Response *http_response;
    Z_HTTP_Header *http_headers = 0;
    ODR odr = odr_createmem(ODR_ENCODE);
    int exit_code = 0;
    int no_urls = 0;

    while ((ret = options("hH:p:u:x:", argv, argc, &arg))
           != YAZ_OPTIONS_EOF)
    {
        switch (ret)
        {
        case 'h':
            usage();
            break;
        case 'H':
            if (!strchr(arg, '='))
            {
                yaz_log(YLOG_FATAL, "bad header option (missing =): %s\n", arg);
                exit_code = 1;
            }
            else
            {
                char *cp = strchr(arg, '=');
                char *name = odr_malloc(odr, 1 + cp - arg);
                char *value = cp + 1;
                memcpy(name, arg, cp - arg);
                name[cp - arg] = '\0';
                z_HTTP_header_add(odr, &http_headers, name, value);
            }
            break;
        case 'p':
            xfree(post_buf);
            post_buf = get_file(arg, &post_len);
            method = "POST";
            break;
        case 'u':
            if (strchr(arg, '/'))
            {
                char *cp = strchr(arg, '/');
                char *user = odr_malloc(odr, 1 + cp - arg);
                char *password = cp + 1;
                memcpy(user, arg, cp - arg);
                user[cp - arg] = '\0';
                z_HTTP_header_add_basic_auth(odr, &http_headers, user,
                                             password);
            }
            else
                z_HTTP_header_add_basic_auth(odr, &http_headers, arg, 0);
            break;
        case 'x':
            yaz_url_set_proxy(p, arg);
            break;
        case 0:
            http_response = yaz_url_exec(p, arg, method, http_headers,
                                         post_buf, post_len);
            if (!http_response)
                exit_code = 1;
            else
            {
                fwrite(http_response->content_buf, 1,
                       http_response->content_len, stdout);
            }
            no_urls++;
            break;
        default:
            usage();
        }
    }
    yaz_url_destroy(p);
    odr_destroy(odr);
    if (no_urls == 0)
        usage();
    exit(exit_code);
}

/*
{'fraction_non_alphanumeric': 0.13007550933181364, 'fraction_numerical': 0.008690696680438809, 'mean_word_length': 1.4761904761904763, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2795790', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2331, 'n_words': 560}