Happy new year
[pazpar2-moved-to-github.git] / src / http.h
1 /* This file is part of Pazpar2.
2    Copyright (C) Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #ifndef HTTP_H
21 #define HTTP_H
22
23 #include <yaz/wrbuf.h>
24 #include <yaz/timing.h>
25
26 #include "eventl.h"
27 // Generic I/O buffer
28 struct http_buf;
29 typedef struct http_channel_observer_s *http_channel_observer_t;
30
31 typedef struct http_server *http_server_t;
32 typedef struct http_sessions *http_sessions_t;
33
34 struct http_channel
35 {
36     IOCHAN iochan;
37     struct http_buf *iqueue;
38     struct http_buf *oqueue;
39     char version[10];
40     struct http_proxy *proxy;
41     enum
42     {
43         Http_Idle,
44         Http_Busy      // Don't process new HTTP requests while we're busy
45     } state;
46     int keep_alive;
47     NMEM nmem;
48     WRBUF wrbuf;
49     yaz_timing_t yt;
50     struct http_request *request;
51     struct http_response *response;
52     struct http_channel *next; // for freelist
53     char addr[256]; // forwarded address
54     http_channel_observer_t observers;
55     struct conf_server *server;
56     http_server_t http_server;
57     http_sessions_t http_sessions;
58 };
59
60 struct http_proxy //  attached to iochan for proxy connection
61 {
62     IOCHAN iochan;
63     struct http_channel *channel;
64     struct http_buf *oqueue;
65     int first_response;
66 };
67
68 struct http_header
69 {
70     char *name;
71     char *value;
72     struct http_header *next;
73 };
74
75 struct http_argument
76 {
77     char *name;
78     char *value;
79     struct http_argument *next;
80 };
81
82 struct http_request
83 {
84     struct http_channel *channel;
85     char http_version[20];
86     char method[20];
87     char *path;
88     char *search;
89     char *content_buf;
90     int content_len;
91     struct http_header *headers;
92     struct http_argument *arguments;
93 };
94
95 struct http_response
96 {
97     char code[4];
98     char *msg;
99     struct http_channel *channel;
100     struct http_header *headers;
101     char *payload;
102     char *content_type;
103 };
104
105 void http_mutex_init(struct conf_server *server);
106 void http_server_destroy(http_server_t hs);
107
108 void http_set_proxyaddr(const char *url, struct conf_server *ser);
109 int http_init(struct conf_server *ser, const char *record_fname);
110 void http_close_server(struct conf_server *ser);
111 void http_addheader(struct http_response *r,
112                     const char *name, const char *value);
113 const char *http_lookup_header(struct http_header *header,
114                                const char *name);
115 struct http_header * http_header_append(struct http_channel *ch,
116                                         struct http_header * hp,
117                                         const char *name,
118                                         const char *value);
119 const char *http_argbyname(struct http_request *r, const char *name);
120 const char *http_headerbyname(struct http_header *r, const char *name);
121 struct http_response *http_create_response(struct http_channel *c);
122 void http_send_response(struct http_channel *c);
123 void urlencode(const char *i, char *o);
124
125 typedef void (*http_channel_destroy_t)(void *data, struct http_channel *c,
126                                        void *data2);
127
128 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
129                                           http_channel_destroy_t);
130 void http_observer_set_data2(http_channel_observer_t obs, void *data2);
131
132 void http_remove_observer(http_channel_observer_t obs);
133 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs);
134
135 void http_command(struct http_channel *c);
136
137 http_sessions_t http_sessions_create(void);
138 void http_sessions_destroy(http_sessions_t hs);
139
140 #endif
141
142 /*
143  * Local variables:
144  * c-basic-offset: 4
145  * c-file-style: "Stroustrup"
146  * indent-tabs-mode: nil
147  * End:
148  * vim: shiftwidth=4 tabstop=8 expandtab
149  */
150