Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / src / zgdu.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zgdu.c
7  * \brief Implements HTTP and Z39.50 encoding and decoding.
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <string.h>
14 #include <yaz/odr.h>
15 #include <yaz/zgdu.h>
16
17 int z_GDU(ODR o, Z_GDU **p, int opt, const char *name)
18 {
19     if (o->direction == ODR_DECODE) {
20         *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
21         if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
22         {
23             (*p)->which = Z_GDU_HTTP_Response;
24             return yaz_decode_http_response(o, &(*p)->u.HTTP_Response);
25
26         }
27         else if (o->size > 5 &&
28             o->buf[0] >= 0x20 && o->buf[0] < 0x7f
29             && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
30             && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
31             && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
32         {
33             (*p)->which = Z_GDU_HTTP_Request;
34             return yaz_decode_http_request(o, &(*p)->u.HTTP_Request);
35         }
36         else
37         {
38             (*p)->which = Z_GDU_Z3950;
39             return z_APDU(o, &(*p)->u.z3950, opt, 0);
40         }
41     }
42     else /* ENCODE or PRINT */
43     {
44         switch((*p)->which)
45         {
46         case Z_GDU_HTTP_Response:
47             return yaz_encode_http_response(o, (*p)->u.HTTP_Response);
48         case Z_GDU_HTTP_Request:
49             return yaz_encode_http_request(o, (*p)->u.HTTP_Request);
50         case Z_GDU_Z3950:
51             return z_APDU(o, &(*p)->u.z3950, opt, 0);
52         }
53     }
54     return 0;
55 }
56
57 /*
58  * Local variables:
59  * c-basic-offset: 4
60  * c-file-style: "Stroustrup"
61  * indent-tabs-mode: nil
62  * End:
63  * vim: shiftwidth=4 tabstop=8 expandtab
64  */
65