Beispiel #1
0
Datei: zget.c Projekt: nla/yaz
Z_External *zget_init_diagnostics_octet(ODR odr, int error,
                                        const char *addinfo)
{
    Z_External *x, *x2;
    Z_OtherInformation *u;
    Z_OtherInformationUnit *l;
    Z_DiagnosticFormat *d;
    Z_DiagnosticFormat_s *e;
    char *octet_buf;
    int octet_len;
    ODR encode;

    u = (Z_OtherInformation *) odr_malloc(odr, sizeof *u);
    u->num_elements = 1;
    u->list = (Z_OtherInformationUnit**) odr_malloc(odr, sizeof *u->list);
    u->list[0] = (Z_OtherInformationUnit*) odr_malloc(odr, sizeof *u->list[0]);
    l = u->list[0];
    l->category = 0;
    l->which = Z_OtherInfo_externallyDefinedInfo;

    x2 = (Z_External*) odr_malloc(odr, sizeof *x);
    l->information.externallyDefinedInfo = x2;
    x2->descriptor = 0;
    x2->indirect_reference = 0;

    x2->direct_reference = odr_oiddup(odr, yaz_oid_diagset_diag_1);
    x2->which = Z_External_diag1;

    d = (Z_DiagnosticFormat*) odr_malloc(odr, sizeof *d);
    x2->u.diag1 = d;
    d->num = 1;
    d->elements = (Z_DiagnosticFormat_s**) odr_malloc(odr, sizeof *d->elements);
    d->elements[0] = (Z_DiagnosticFormat_s*) odr_malloc(odr, sizeof *d->elements[0]);
    e = d->elements[0];

    e->which = Z_DiagnosticFormat_s_defaultDiagRec;
    e->u.defaultDiagRec = zget_DefaultDiagFormat(odr, error, addinfo);
    e->message = 0;

    encode = odr_createmem(ODR_ENCODE);

    z_OtherInformation(encode, &u, 0, 0);

    octet_buf = odr_getbuf(encode, &octet_len, 0);

    x = (Z_External*) odr_malloc(odr, sizeof *x);
    x->descriptor = 0;
    x->indirect_reference = 0;
    x->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_userinfo_1);
    x->which = Z_External_octet;
    x->u.octet_aligned = (Odr_oct *) odr_malloc(odr, sizeof(Odr_oct));
    x->u.octet_aligned->buf = (unsigned char *) odr_malloc(odr, octet_len);
    memcpy(x->u.octet_aligned->buf, octet_buf, octet_len);
    x->u.octet_aligned->len = octet_len;

    odr_destroy(encode);

    return x;
}
Beispiel #2
0
static int compare_attr(Z_AttributeElement *a, Z_AttributeElement *b)
{
    ODR odr_a = odr_createmem(ODR_ENCODE);
    ODR odr_b = odr_createmem(ODR_ENCODE);
    int len_a, len_b;
    char *buf_a, *buf_b;
    int ret;

    z_AttributeElement(odr_a, &a, 0, 0);
    z_AttributeElement(odr_b, &b, 0, 0);

    buf_a = odr_getbuf(odr_a, &len_a, 0);
    buf_b = odr_getbuf(odr_b, &len_b, 0);

    ret = yaz_memcmp(buf_a, buf_b, len_a, len_b);

    odr_destroy(odr_a);
    odr_destroy(odr_b);
    return ret;
}
Beispiel #3
0
Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
                              const char *method,
                              Z_HTTP_Header *user_headers,
                              const char *buf, size_t len)
{
    Z_HTTP_Response *res = 0;
    int number_of_redirects = 0;

    yaz_cookies_reset(p->cookies);
    wrbuf_rewind(p->w_error);
    while (1)
    {
        void *add;
        COMSTACK conn = 0;
        int code;
        const char *location = 0;
        char *http_user = 0;
        char *http_pass = 0;
        char *uri_lean = 0;
        Z_GDU *gdu;

        extract_user_pass(p->odr_out->mem, uri, &uri_lean,
                          &http_user, &http_pass);

        gdu = z_get_HTTP_Request_uri(p->odr_out, uri_lean, 0, p->proxy ? 1 : 0);
        gdu->u.HTTP_Request->method = odr_strdup(p->odr_out, method);

        yaz_cookies_request(p->cookies, p->odr_out, gdu->u.HTTP_Request);
        for ( ; user_headers; user_headers = user_headers->next)
        {
            /* prefer new Host over user-supplied Host */
            if (!strcmp(user_headers->name, "Host"))
                ;
            /* prefer user-supplied User-Agent over YAZ' own */
            else if (!strcmp(user_headers->name, "User-Agent"))
                z_HTTP_header_set(p->odr_out, &gdu->u.HTTP_Request->headers,
                                  user_headers->name, user_headers->value);
            else
                z_HTTP_header_add(p->odr_out, &gdu->u.HTTP_Request->headers,
                                  user_headers->name, user_headers->value);
        }
        if (http_user && http_pass)
            z_HTTP_header_add_basic_auth(p->odr_out,
                                         &gdu->u.HTTP_Request->headers,
                                         http_user, http_pass);

        res = 0;
        if (buf && len)
        {
            gdu->u.HTTP_Request->content_buf = (char *) buf;
            gdu->u.HTTP_Request->content_len = len;
        }
        if (!z_GDU(p->odr_out, &gdu, 0, 0))
        {
            wrbuf_printf(p->w_error, "Can not encode HTTP request for URL %s",
                         uri);
            log_warn(p);
            return 0;
        }
        conn = cs_create_host_proxy(uri_lean, 1, &add, p->proxy);
        if (!conn)
        {
            wrbuf_printf(p->w_error, "Can not resolve URL %s", uri);
            log_warn(p);
        }
        else if (cs_connect(conn, add) < 0)
        {
            wrbuf_printf(p->w_error, "Can not connect to URL %s", uri);
            log_warn(p);
        }
        else
        {
            int len;
            char *buf = odr_getbuf(p->odr_out, &len, 0);

            if (p->verbose)
                fwrite(buf, 1, len, stdout);

            if (cs_put(conn, buf, len) < 0)
            {
                wrbuf_printf(p->w_error, "cs_put fail for URL %s", uri);
                log_warn(p);
            }
            else
            {
                char *netbuffer = 0;
                int netlen = 0;
                int cs_res = cs_get(conn, &netbuffer, &netlen);
                if (cs_res <= 0)
                {
                    wrbuf_printf(p->w_error, "cs_get failed for URL %s", uri);
                    log_warn(p);
                }
                else
                {
                    Z_GDU *gdu;
                    if (p->verbose)
                        fwrite(netbuffer, 1, cs_res, stdout);
                    odr_setbuf(p->odr_in, netbuffer, cs_res, 0);
                    if (!z_GDU(p->odr_in, &gdu, 0, 0)
                            || gdu->which != Z_GDU_HTTP_Response)
                    {
                        wrbuf_printf(p->w_error, "HTTP decoding fail for "
                                     "URL %s", uri);
                        log_warn(p);
                    }
                    else
                    {
                        res = gdu->u.HTTP_Response;
                    }
                }
                xfree(netbuffer);
            }
        }
        if (conn)
            cs_close(conn);
        if (!res)
            break;
        code = res->code;
        location = z_HTTP_header_lookup(res->headers, "Location");
        if (++number_of_redirects <= p->max_redirects &&
                location && (code == 301 || code == 302 || code == 307))
        {
            int host_change = 0;
            const char *nlocation = yaz_check_location(p->odr_in, uri,
                                    location, &host_change);

            odr_reset(p->odr_out);
            uri = odr_strdup(p->odr_out, nlocation);
        }
        else
            break;
        yaz_cookies_response(p->cookies, res);
        odr_reset(p->odr_in);
    }
    return res;
}