aboutsummaryrefslogtreecommitdiff
path: root/src/include/microspdy.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/microspdy.h')
-rw-r--r--src/include/microspdy.h1275
1 files changed, 1275 insertions, 0 deletions
diff --git a/src/include/microspdy.h b/src/include/microspdy.h
new file mode 100644
index 00000000..ac9123b8
--- /dev/null
+++ b/src/include/microspdy.h
@@ -0,0 +1,1275 @@
1/*
2 This file is part of libmicrospdy
3 Copyright (C) 2012, 2013 Christian Grothoff
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file microspdy.h
21 * @brief public interface to libmicrospdy
22 * @author Andrey Uzunov
23 * @author Christian Grothoff
24 *
25 * All symbols defined in this header start with SPDY_. libmisrospdy is a small
26 * SPDY daemon library. The application can start multiple daemons
27 * and they are independent.<p>
28 *
29 * The header file defines various constants used by the SPDY and the HTTP protocol.
30 * This does not mean that the lib actually interprets all of these
31 * values. Not everything is implemented. The provided constants are exported as a convenience
32 * for users of the library. The lib does not verify that provided
33 * HTTP headers and if their values conform to the SPDY protocol,
34 * it only checks if the required headers for the SPDY requests and
35 * responses are provided.<p>
36 *
37 * The library uses just a single thread.<p>
38 *
39 * Before including "microspdy.h" you should add the necessary
40 * includes to define the types used in this file (which headers are needed may
41 * depend on your platform; for possible suggestions consult
42 * "platform.h" in the libmicrospdy distribution).<p>
43 *
44 * All of the functions returning SPDY_YES/SPDY_NO return
45 * SPDY_INPUT_ERROR when any of the parameters are invalid, e.g.,
46 * required parameter is NULL.
47 */
48#ifndef SPDY_MICROSPDY_H
49#define SPDY_MICROSPDY_H
50
51#include <zlib.h>
52#include <stdbool.h>
53
54/**
55 * return code for "YES".
56 */
57#define SPDY_YES 1
58
59/**
60 * return code for "NO".
61 */
62#define SPDY_NO 0
63
64/**
65 * return code for error when input parameters are wrong. To be returned
66 * only by functions which return int. The others will return NULL on
67 * input error.
68 */
69#define SPDY_INPUT_ERROR -1
70
71/**
72 * SPDY version supported by the lib.
73 */
74#define SPDY_VERSION 3
75
76/**
77 * The maximum allowed size (without 8 byte headers) of
78 * SPDY frames (value length) is 8192. The lib will accept and
79 * send frames with length at most this value here.
80 */
81#define SPDY_MAX_SUPPORTED_FRAME_SIZE 8192
82
83/**
84 * HTTP response codes.
85 */
86#define SPDY_HTTP_CONTINUE 100
87#define SPDY_HTTP_SWITCHING_PROTOCOLS 101
88#define SPDY_HTTP_PROCESSING 102
89
90#define SPDY_HTTP_OK 200
91#define SPDY_HTTP_CREATED 201
92#define SPDY_HTTP_ACCEPTED 202
93#define SPDY_HTTP_NON_AUTHORITATIVE_INFORMATION 203
94#define SPDY_HTTP_NO_CONTENT 204
95#define SPDY_HTTP_RESET_CONTENT 205
96#define SPDY_HTTP_PARTIAL_CONTENT 206
97#define SPDY_HTTP_MULTI_STATUS 207
98
99#define SPDY_HTTP_MULTIPLE_CHOICES 300
100#define SPDY_HTTP_MOVED_PERMANENTLY 301
101#define SPDY_HTTP_FOUND 302
102#define SPDY_HTTP_SEE_OTHER 303
103#define SPDY_HTTP_NOT_MODIFIED 304
104#define SPDY_HTTP_USE_PROXY 305
105#define SPDY_HTTP_SWITCH_PROXY 306
106#define SPDY_HTTP_TEMPORARY_REDIRECT 307
107
108#define SPDY_HTTP_BAD_REQUEST 400
109#define SPDY_HTTP_UNAUTHORIZED 401
110#define SPDY_HTTP_PAYMENT_REQUIRED 402
111#define SPDY_HTTP_FORBIDDEN 403
112#define SPDY_HTTP_NOT_FOUND 404
113#define SPDY_HTTP_METHOD_NOT_ALLOWED 405
114#define SPDY_HTTP_METHOD_NOT_ACCEPTABLE 406
115#define SPDY_HTTP_PROXY_AUTHENTICATION_REQUIRED 407
116#define SPDY_HTTP_REQUEST_TIMEOUT 408
117#define SPDY_HTTP_CONFLICT 409
118#define SPDY_HTTP_GONE 410
119#define SPDY_HTTP_LENGTH_REQUIRED 411
120#define SPDY_HTTP_PRECONDITION_FAILED 412
121#define SPDY_HTTP_REQUEST_ENTITY_TOO_LARGE 413
122#define SPDY_HTTP_REQUEST_URI_TOO_LONG 414
123#define SPDY_HTTP_UNSUPPORTED_MEDIA_TYPE 415
124#define SPDY_HTTP_REQUESTED_RANGE_NOT_SATISFIABLE 416
125#define SPDY_HTTP_EXPECTATION_FAILED 417
126#define SPDY_HTTP_UNPROCESSABLE_ENTITY 422
127#define SPDY_HTTP_LOCKED 423
128#define SPDY_HTTP_FAILED_DEPENDENCY 424
129#define SPDY_HTTP_UNORDERED_COLLECTION 425
130#define SPDY_HTTP_UPGRADE_REQUIRED 426
131#define SPDY_HTTP_NO_RESPONSE 444
132#define SPDY_HTTP_RETRY_WITH 449
133#define SPDY_HTTP_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS 450
134#define SPDY_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS 451
135
136#define SPDY_HTTP_INTERNAL_SERVER_ERROR 500
137#define SPDY_HTTP_NOT_IMPLEMENTED 501
138#define SPDY_HTTP_BAD_GATEWAY 502
139#define SPDY_HTTP_SERVICE_UNAVAILABLE 503
140#define SPDY_HTTP_GATEWAY_TIMEOUT 504
141#define SPDY_HTTP_HTTP_VERSION_NOT_SUPPORTED 505
142#define SPDY_HTTP_VARIANT_ALSO_NEGOTIATES 506
143#define SPDY_HTTP_INSUFFICIENT_STORAGE 507
144#define SPDY_HTTP_BANDWIDTH_LIMIT_EXCEEDED 509
145#define SPDY_HTTP_NOT_EXTENDED 510
146
147/**
148 * HTTP headers are used in SPDY, but all of them MUST be lowercase.
149 * Some are not valid in SPDY and MUST not be used
150 */
151#define SPDY_HTTP_HEADER_ACCEPT "accept"
152#define SPDY_HTTP_HEADER_ACCEPT_CHARSET "accept-charset"
153#define SPDY_HTTP_HEADER_ACCEPT_ENCODING "accept-encoding"
154#define SPDY_HTTP_HEADER_ACCEPT_LANGUAGE "accept-language"
155#define SPDY_HTTP_HEADER_ACCEPT_RANGES "accept-ranges"
156#define SPDY_HTTP_HEADER_AGE "age"
157#define SPDY_HTTP_HEADER_ALLOW "allow"
158#define SPDY_HTTP_HEADER_AUTHORIZATION "authorization"
159#define SPDY_HTTP_HEADER_CACHE_CONTROL "cache-control"
160/* Connection header is forbidden in SPDY */
161#define SPDY_HTTP_HEADER_CONNECTION "connection"
162#define SPDY_HTTP_HEADER_CONTENT_ENCODING "content-encoding"
163#define SPDY_HTTP_HEADER_CONTENT_LANGUAGE "content-language"
164#define SPDY_HTTP_HEADER_CONTENT_LENGTH "content-length"
165#define SPDY_HTTP_HEADER_CONTENT_LOCATION "content-location"
166#define SPDY_HTTP_HEADER_CONTENT_MD5 "content-md5"
167#define SPDY_HTTP_HEADER_CONTENT_RANGE "content-range"
168#define SPDY_HTTP_HEADER_CONTENT_TYPE "content-type"
169#define SPDY_HTTP_HEADER_COOKIE "cookie"
170#define SPDY_HTTP_HEADER_DATE "date"
171#define SPDY_HTTP_HEADER_ETAG "etag"
172#define SPDY_HTTP_HEADER_EXPECT "expect"
173#define SPDY_HTTP_HEADER_EXPIRES "expires"
174#define SPDY_HTTP_HEADER_FROM "from"
175/* Host header is forbidden in SPDY */
176#define SPDY_HTTP_HEADER_HOST "host"
177#define SPDY_HTTP_HEADER_IF_MATCH "if-match"
178#define SPDY_HTTP_HEADER_IF_MODIFIED_SINCE "if-modified-since"
179#define SPDY_HTTP_HEADER_IF_NONE_MATCH "if-none-match"
180#define SPDY_HTTP_HEADER_IF_RANGE "if-range"
181#define SPDY_HTTP_HEADER_IF_UNMODIFIED_SINCE "if-unmodified-since"
182/* Keep-Alive header is forbidden in SPDY */
183#define SPDY_HTTP_HEADER_KEEP_ALIVE "keep-alive"
184#define SPDY_HTTP_HEADER_LAST_MODIFIED "last-modified"
185#define SPDY_HTTP_HEADER_LOCATION "location"
186#define SPDY_HTTP_HEADER_MAX_FORWARDS "max-forwards"
187#define SPDY_HTTP_HEADER_PRAGMA "pragma"
188#define SPDY_HTTP_HEADER_PROXY_AUTHENTICATE "proxy-authenticate"
189#define SPDY_HTTP_HEADER_PROXY_AUTHORIZATION "proxy-authorization"
190/* Proxy-Connection header is forbidden in SPDY */
191#define SPDY_HTTP_HEADER_PROXY_CONNECTION "proxy-connection"
192#define SPDY_HTTP_HEADER_RANGE "range"
193#define SPDY_HTTP_HEADER_REFERER "referer"
194#define SPDY_HTTP_HEADER_RETRY_AFTER "retry-after"
195#define SPDY_HTTP_HEADER_SERVER "server"
196#define SPDY_HTTP_HEADER_SET_COOKIE "set-cookie"
197#define SPDY_HTTP_HEADER_SET_COOKIE2 "set-cookie2"
198#define SPDY_HTTP_HEADER_TE "te"
199#define SPDY_HTTP_HEADER_TRAILER "trailer"
200/* Transfer-Encoding header is forbidden in SPDY */
201#define SPDY_HTTP_HEADER_TRANSFER_ENCODING "transfer-encoding"
202#define SPDY_HTTP_HEADER_UPGRADE "upgrade"
203#define SPDY_HTTP_HEADER_USER_AGENT "user-agent"
204#define SPDY_HTTP_HEADER_VARY "vary"
205#define SPDY_HTTP_HEADER_VIA "via"
206#define SPDY_HTTP_HEADER_WARNING "warning"
207#define SPDY_HTTP_HEADER_WWW_AUTHENTICATE "www-authenticate"
208
209/**
210 * HTTP versions (a value must be provided in SPDY requests/responses).
211 */
212#define SPDY_HTTP_VERSION_1_0 "HTTP/1.0"
213#define SPDY_HTTP_VERSION_1_1 "HTTP/1.1"
214
215/**
216 * HTTP methods
217 */
218#define SPDY_HTTP_METHOD_CONNECT "CONNECT"
219#define SPDY_HTTP_METHOD_DELETE "DELETE"
220#define SPDY_HTTP_METHOD_GET "GET"
221#define SPDY_HTTP_METHOD_HEAD "HEAD"
222#define SPDY_HTTP_METHOD_OPTIONS "OPTIONS"
223#define SPDY_HTTP_METHOD_POST "POST"
224#define SPDY_HTTP_METHOD_PUT "PUT"
225#define SPDY_HTTP_METHOD_TRACE "TRACE"
226
227/**
228 * HTTP POST encodings, see also
229 * http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4
230 */
231#define SPDY_HTTP_POST_ENCODING_FORM_URLENCODED "application/x-www-form-urlencoded"
232#define SPDY_HTTP_POST_ENCODING_MULTIPART_FORMDATA "multipart/form-data"
233
234
235/**
236 * Handle for the daemon (listening on a socket).
237 */
238struct SPDY_Daemon;
239
240
241/**
242 * Handle for a SPDY session/connection.
243 */
244struct SPDY_Session;
245
246
247/**
248 * Handle for a SPDY request sent by a client. The structure has pointer
249 * to the session's handler
250 */
251struct SPDY_Request;
252
253
254/**
255 * Handle for a response containing HTTP headers and data to be sent.
256 * The structure has pointer to the session's handler
257 * for this response.
258 */
259struct SPDY_Response;
260
261
262/**
263 * Collection of tuples of an HTTP header and values used in requests
264 * and responses.
265 */
266struct SPDY_NameValue;
267
268
269/**
270 * Collection of tuples of a SPDY setting ID, value
271 * and flags used to control the sessions.
272 */
273struct SPDY_Settings;
274
275
276/**
277 * SPDY daemon options. Passed in the varargs portion of
278 * SPDY_start_daemon to customize the daemon. Each option must
279 * be followed by a value of a specific type.<p>
280 *
281 * The values are used internally as flags, that is why they must be
282 * powers of 2.
283 */
284enum SPDY_DAEMON_OPTION
285{
286
287 /**
288 * No more options / last option. This is used
289 * to terminate the VARARGs list.
290 */
291 SPDY_DAEMON_OPTION_END = 0,
292
293 /**
294 * Set a custom timeout for all connections. Must be followed by
295 * a number of seconds, given as an 'unsigned int'. Use
296 * zero for no timeout.
297 */
298 SPDY_DAEMON_OPTION_SESSION_TIMEOUT = 1,
299
300 /**
301 * Bind daemon to the supplied sockaddr. This option must be
302 * followed by a 'struct sockaddr *'. The 'struct sockaddr*'
303 * should point to a 'struct sockaddr_in6' or to a
304 * 'struct sockaddr_in'.
305 */
306 SPDY_DAEMON_OPTION_SOCK_ADDR = 2,
307
308 /**
309 * Flags for the daemon. Must be followed by a SPDY_DAEMON_FLAG value
310 * which is the result of bitwise OR of desired flags.
311 */
312 SPDY_DAEMON_OPTION_FLAGS = 4,
313};
314
315
316/**
317 * Flags for starting SPDY daemon. They are used to set some settings
318 * for the daemon, which do not require values.
319 */
320enum SPDY_DAEMON_FLAG
321{
322 /**
323 * No flags selected.
324 */
325 SPDY_DAEMON_FLAG_NO = 0,
326
327 /**
328 * The server will bind only on IPv6 addresses. If the flag is set and
329 * the daemon is provided with IPv4 address or IPv6 is not supported,
330 * starting daemon will fail.
331 */
332 SPDY_DAEMON_FLAG_ONLY_IPV6 = 1,
333};
334
335
336/**
337 * SPDY settings IDs sent by both client and server in SPDY SETTINGS frame.
338 * They affect the whole SPDY session. Defined in SPDY Protocol - Draft 3.
339 */
340enum SPDY_SETTINGS
341{
342
343 /**
344 * Allows the sender to send its expected upload bandwidth on this
345 * channel. This number is an estimate. The value should be the
346 * integral number of kilobytes per second that the sender predicts
347 * as an expected maximum upload channel capacity.
348 */
349 SPDY_SETTINGS_UPLOAD_BANDWIDTH = 1,
350
351 /**
352 * Allows the sender to send its expected download bandwidth on this
353 * channel. This number is an estimate. The value should be the
354 * integral number of kilobytes per second that the sender predicts as
355 * an expected maximum download channel capacity.
356 */
357 SPDY_SETTINGS_DOWNLOAD_BANDWIDTH = 2,
358
359 /**
360 * Allows the sender to send its expected round-trip-time on this
361 * channel. The round trip time is defined as the minimum amount of
362 * time to send a control frame from this client to the remote and
363 * receive a response. The value is represented in milliseconds.
364 */
365 SPDY_SETTINGS_ROUND_TRIP_TIME = 3,
366
367 /**
368 * Allows the sender to inform the remote endpoint the maximum number
369 * of concurrent streams which it will allow. By default there is no
370 * limit. For implementors it is recommended that this value be no
371 * smaller than 100.
372 */
373 SPDY_SETTINGS_MAX_CONCURRENT_STREAMS = 4,
374
375 /**
376 * Allows the sender to inform the remote endpoint of the current TCP
377 * CWND value.
378 */
379 SPDY_SETTINGS_CURRENT_CWND = 5,
380
381 /**
382 * Allows the sender to inform the remote endpoint the retransmission
383 * rate (bytes retransmitted / total bytes transmitted).
384 */
385 SPDY_SETTINGS_DOWNLOAD_RETRANS_RATE = 6,
386
387 /**
388 * Allows the sender to inform the remote endpoint the initial window
389 * size (in bytes) for new streams.
390 */
391 SPDY_SETTINGS_INITIAL_WINDOW_SIZE = 7,
392
393 /**
394 * Allows the server to inform the client if the new size of the
395 * client certificate vector.
396 */
397 SPDY_SETTINGS_CLIENT_CERTIFICATE_VECTOR_SIZE = 8,
398};
399
400
401/**
402 * Flags for each individual SPDY setting in the SPDY SETTINGS frame.
403 * They affect only one setting to which they are set.
404 * Defined in SPDY Protocol - Draft 3.
405 */
406enum SPDY_FLAG_SETTINGS{
407
408 /**
409 * When set, the sender of this SETTINGS frame is requesting that the
410 * recipient persist the ID/Value and return it in future SETTINGS
411 * frames sent from the sender to this recipient. Because persistence
412 * is only implemented on the client, this flag is only sent by the
413 * server.
414 */
415 SPDY_FLAG_SETTINGS_PERSIST_VALUE = 1,
416
417 /**
418 * When set, the sender is notifying the recipient that this ID/Value
419 * pair was previously sent to the sender by the recipient with the
420 * SPDY_FLAG_SETTINGS_PERSIST_VALUE, and the sender is returning it.
421 * Because persistence is only implemented on the client, this flag is
422 * only sent by the client.
423 */
424 SPDY_FLAG_SETTINGS_PERSISTED = 2,
425};
426
427
428/**
429 * Flag associated with a whole SPDY SETTINGS frame. Affect all the
430 * settings in the frame. Defined in SPDY Protocol - Draft 3.
431 */
432enum SPDY_FLAG_SETTINGS_FRAME
433{
434
435 /**
436 * When set, the client should clear any previously persisted SETTINGS
437 * ID/Value pairs. If this frame contains ID/Value pairs with the
438 * SPDY_FLAG_SETTINGS_PERSIST_VALUE set, then the client will first
439 * clear its existing, persisted settings, and then persist the values
440 * with the flag set which are contained within this frame. Because
441 * persistence is only implemented on the client, this flag can only
442 * be used when the sender is the server.
443 */
444 SPDY_FLAG_SETTINGS_CLEAR_SETTINGS = 1,
445};
446
447
448/**
449 * SPDY settings function options. Passed in the varargs portion of
450 * SPDY_SettingsReceivedCallback and SPDY_send_settings to customize
451 * more the settings handling. Each option must
452 * be followed by a value of a specific type.<p>
453 *
454 * The values are used internally as flags, that is why they must be
455 * powers of 2.
456 */
457enum SPDY_SETTINGS_OPTION
458{
459
460 /**
461 * No more options / last option. This is used
462 * to terminate the VARARGs list.
463 */
464 SPDY_SETTINGS_OPTION_END = 0,
465};
466
467
468/**
469 * Used as a parameter for SPDY_ResponseResultCallback and shows if the
470 * response was actually written to the TLS socket or discarded by the
471 * lib for any reason (and respectively the reason).
472 */
473enum SPDY_RESPONSE_RESULT
474{
475
476 /**
477 * The lib has written the full response to the TLS socket.
478 */
479 SPDY_RESPONSE_RESULT_SUCCESS = 0,
480
481 /**
482 * The session is being closed, so the data is being discarded
483 */
484 SPDY_RESPONSE_RESULT_SESSION_CLOSED = 1,
485
486 /**
487 * The stream for this response has been closed. May happen when the
488 * sender had sent first SYN_STREAM and after that RST_STREAM.
489 */
490 SPDY_RESPONSE_RESULT_STREAM_CLOSED = 2,
491};
492
493
494/**
495 * Callback for serious error condition. The default action is to print
496 * an error message and abort().
497 *
498 * @param cls user specified value
499 * @param file where the error occured
500 * @param line where the error occured
501 * @param reason error details message, may be NULL
502 */
503typedef void
504(*SPDY_PanicCallback) (void * cls,
505 const char * file,
506 unsigned int line,
507 const char * reason);
508
509
510/**
511 * Callback for new SPDY session established by a client. Called
512 * immediately after the TCP connection was established.
513 *
514 * @param cls client-defined closure
515 * @param session handler for the new SPDY session
516 */
517typedef void
518(*SPDY_NewSessionCallback) (void * cls,
519 struct SPDY_Session * session);
520
521
522/**
523 * Callback for closed session. Called after the TCP connection was
524 * closed. In this callback function the user has the last
525 * chance to access the SPDY_Session structure. After that the latter
526 * will be cleaned!
527 *
528 * @param cls client-defined closure
529 * @param session handler for the closed SPDY session
530 * @param by_client SPDY_YES if the session close was initiated by the
531 * client;
532 * SPDY_NO if closed by the server
533 */
534typedef void
535(*SPDY_SessionClosedCallback) (void * cls,
536 struct SPDY_Session * session,
537 int by_client);
538
539
540/**
541 * Iterator over name-value pairs.
542 *
543 * @param cls client-defined closure
544 * @param name of the pair
545 * @param value of the pair
546 * @return SPDY_YES to continue iterating,
547 * SPDY_NO to abort the iteration
548 */
549typedef int
550(*SPDY_NameValueIterator) (void * cls,
551 const char * name,
552 const char * const * value,
553 int num_values);
554
555
556/**
557 * Callback for received SPDY request.
558 *
559 * @param cls client-defined closure
560 * @param request handler. The request object is required for
561 * sending responses.
562 * @param priority of the SPDY stream which the request was
563 * sent over
564 * @param method HTTP method
565 * @param path HTTP path
566 * @param version HTTP version just like in HTTP request/response:
567 * "HTTP/1.0" or "HTTP/1.1" currently
568 * @param host called host as in HTTP
569 * @param scheme used ("http" or "https"). In SPDY 3 it is only "https".
570 * @param headers other HTTP headers from the request
571 */
572typedef void (*SPDY_NewRequestCallback) (void * cls,
573 struct SPDY_Request * request,
574 uint8_t priority,
575 const char * method,
576 const char * path,
577 const char * version,
578 const char * host,
579 const char * scheme,
580 struct SPDY_NameValue * headers);
581
582
583/**
584 * Callback for received new data chunk from the POST data of a given
585 * request.
586 *
587 * @param cls client-defined closure
588 * @param request handler
589 * @param buf data chunk from the POST data
590 * @param size the size of the data chunk 'buf' in bytes
591 * @param more false if this is the last chunk from the POST data. Note:
592 * true does not mean that more data will come, exceptional
593 * situation is possible
594 * @return SPDY_YES to continue calling the function,
595 * SPDY_NO to stop calling the function for this request
596 */
597typedef int (*SPDY_NewPOSTDataCallback) (void * cls,
598 struct SPDY_Request *request,
599 const void * buf,
600 size_t size,
601 bool more);
602// How about passing POST encoding information
603// here as well?
604//TODO
605
606
607/**
608 * Callback to be used with SPDY_build_response_with_callback. The
609 * callback will be called when the lib wants to write to the TLS socket.
610 * The application should provide the data to be sent.
611 *
612 * @param cls client-defined closure
613 * @param max maximum number of bytes that are allowed to be written
614 * to the buffer.
615 * @param more true if more data will be sent (i.e. the function must
616 * be calleed again),
617 * false if this is the last chunk, the lib will close
618 * the stream
619 * @return number of bytes written to buffer. On error the call MUST
620 * return value less than 0 to indicate the library.
621 */
622typedef ssize_t (*SPDY_ResponseCallback) (void * cls,
623 void * buffer,
624 size_t max,
625 bool * more);
626
627
628/**
629 * Callback to be called when the last bytes from the response was sent
630 * to the client or when the response was discarded from the lib. This
631 * callback is a very good place to discard the request and the response
632 * objects, if they will not be reused (e.g., sending the same response
633 * again). If the stream is closed it is safe to discard the request
634 * object.
635 *
636 * @param cls client-defined closure
637 * @param response handler to the response that was just sent
638 * @param request handler to the request for which the response was sent
639 * @param status shows if actually the response was sent or it was
640 * discarded by the lib for any reason (e.g., closing session,
641 * closing stream, stopping daemon, etc.). It is possible that
642 * status indicates an error but parts of the response headers
643 * and/or body (in one
644 * or several frames) were already sent to the client.
645 * @param streamopened indicates if the the stream for this request/
646 * response pair is still opened. If yes, the server may want
647 * to use SPDY push to send something additional to the client
648 * and/or close the stream.
649 */
650typedef void
651(*SPDY_ResponseResultCallback) (void * cls,
652 struct SPDY_Response * response,
653 struct SPDY_Request * request,
654 enum SPDY_RESPONSE_RESULT status,
655 bool streamopened);
656
657
658/**
659 * Callback to notify when SPDY ping response is received.
660 *
661 * @param session handler for which the ping request was sent
662 * @param rtt the timespan between sending ping request and receiving it
663 * from the library
664 */
665typedef void
666(*SPDY_PingCallback) (void * cls,
667 struct SPDY_Session * session,
668 struct timeval * rtt);
669
670
671/**
672 * Iterator over settings ID/Value/Flags tuples.
673 *
674 * @param cls client-defined closure
675 * @param id SPDY settings ID
676 * @param value value for this setting
677 * @param flags flags for this tuple; use
678 * enum SPDY_FLAG_SETTINGS
679 * @return SPDY_YES to continue iterating,
680 * SPDY_NO to abort the iteration
681 */
682typedef int
683(*SPDY_SettingsIterator) (void * cls,
684 enum SPDY_SETTINGS id,
685 int32_t value,
686 uint8_t flags);
687
688
689/**
690 * Callback to notify when SPDY SETTINGS are received from the client.
691 *
692 * @param session handler for which settings are received
693 * @param settings ID/value/flags tuples of the settings
694 * @param flags for the whole settings frame; use
695 * enum SPDY_FLAG_SETTINGS_FRAME
696 * @param ... list of options (type-value pairs,
697 * terminated with SPDY_SETTINGS_OPTION_END).
698 */
699typedef void
700(*SPDY_SettingsReceivedCallback) (struct SPDY_Session * session,
701 struct SPDY_Settings * settings,
702 uint8_t flags,
703 ...);
704
705
706/* Global functions for the library */
707
708
709/**
710 * Init function for the whole library. It MUST be called before any
711 * other function of the library to initialize things like TLS context
712 * and possibly other stuff needed by the lib. Currently the call
713 * always returns SPDY_YES.
714 *
715 * @return SPDY_YES if the library was correctly initialized and its
716 * functions can be used now;
717 * SPDY_NO on error
718 */
719int
720SPDY_init (void);
721
722
723/**
724 * Deinit function for the whole lib. It can be called after finishing
725 * using the library. It frees and cleans up resources allocated in
726 * SPDY_init. Currently the function does not do anything.
727 */
728void
729SPDY_deinit (void);
730
731
732/**
733 * Sets the global error handler to a different implementation. "cb"
734 * will only be called in the case of typically fatal, serious
735 * internal consistency issues. These issues should only arise in the
736 * case of serious memory corruption or similar problems with the
737 * architecture as well as failed assertions. While "cb" is allowed to
738 * return and the lib will then try to continue, this is never safe.
739 *
740 * The default implementation that is used if no panic function is set
741 * simply prints an error message and calls "abort". Alternative
742 * implementations might call "exit" or other similar functions.
743 *
744 * @param cb new error handler
745 * @param cls passed to error handler
746 */
747void
748SPDY_set_panic_func (SPDY_PanicCallback cb,
749 void *cls);
750
751
752/* Daemon functions */
753
754
755/**
756 * Start a SPDY webserver on the given port.
757 *
758 * @param port to bind to. The value is ignored if address structure
759 * is passed as daemon option
760 * @param certfile path to the certificate that will be used by server
761 * @param keyfile path to the keyfile for the certificate
762 * @param nscb callback called when a new SPDY session is
763 * established by a client
764 * @param sccb callback called when a session is closed
765 * @param nrcb callback called when a client sends request
766 * @param npdcb callback called when HTTP POST params are received
767 * after request
768 * @param cls common extra argument to all of the callbacks
769 * @param ... list of options (type-value pairs,
770 * terminated with SPDY_DAEMON_OPTION_END).
771 * @return NULL on error, handle to daemon on success
772 */
773struct SPDY_Daemon *
774SPDY_start_daemon (uint16_t port,
775 const char * certfile,
776 const char * keyfile,
777 SPDY_NewSessionCallback nscb,
778 SPDY_SessionClosedCallback sccb,
779 SPDY_NewRequestCallback nrcb,
780 SPDY_NewPOSTDataCallback npdcb,
781 void * cls,
782 ...);
783
784
785/**
786 * Shutdown the daemon. First all sessions are closed. It is NOT safe
787 * to call this function in user callbacks.
788 *
789 * @param daemon to stop
790 */
791void
792SPDY_stop_daemon (struct SPDY_Daemon *daemon);
793
794
795/**
796 * Obtain the select sets for this daemon. Only those are retrieved,
797 * which some processing should be done for, i.e. not all sockets are
798 * added to write_fd_set.<p>
799 *
800 * It is possible that there is
801 * nothing to be read from a socket but there is data either in the
802 * TLS subsystem's read buffers or in libmicrospdy's read buffers, which
803 * waits for being processed. In such case the file descriptor will be
804 * added to write_fd_set. Since it is very likely for the socket to be
805 * ready for writing, the select used in the application's event loop
806 * will return with success, SPDY_run will be called, the data will be
807 * processed and maybe something will be written to the socket. Without
808 * this behaviour, considering a proper event loop, data may stay in the
809 * buffers, but run is never called.
810 *
811 * @param daemon to get sets from
812 * @param read_fd_set read set
813 * @param write_fd_set write set
814 * @param except_fd_set except set
815 * @return largest FD added to any of the sets
816 */
817int
818SPDY_get_fdset (struct SPDY_Daemon * daemon,
819 fd_set * read_fd_set,
820 fd_set * write_fd_set,
821 fd_set * except_fd_set);
822
823
824/**
825 * Obtain timeout value for select for this daemon. The returned value
826 * is how long select
827 * should at most block, not the timeout value set for connections.
828 *
829 * @param daemon to query for timeout
830 * @param timeout will be set to the timeout value (in seconds)
831 * @return SPDY_YES on success
832 * SPDY_NO if no connections exist that
833 * would necessiate the use of a timeout right now
834 */
835int
836SPDY_get_timeout (struct SPDY_Daemon * daemon,
837 unsigned long long * timeout);
838
839
840/**
841 * Run webserver operations. This method must be called in
842 * the client event loop.
843 *
844 * @param daemon to run
845 */
846void
847SPDY_run (struct SPDY_Daemon *daemon);
848
849
850/* SPDY Session handling functions */
851
852
853/**
854 * Closes a SPDY session. SPDY clients and servers are expected to keep
855 * sessions opened as long as possible. However, the server may want to
856 * close some connections, e.g. if there are too many, to free some
857 * resources. The function can also be used to close a specific session
858 * if the client is not desired.
859 *
860 * @param session handler to be closed
861 */
862void
863SPDY_close_session(struct SPDY_Session * session);
864
865
866/**
867 * Associate a void pointer with a session. The data accessible by the
868 * pointer can later be used wherever the session handler is available.
869 *
870 * @param session handler
871 * @param cls any data pointed by a pointer to be accessible later
872 */
873void
874SPDY_set_cls_to_session(struct SPDY_Session * session,
875 void * cls);
876
877
878/**
879 * Retrieves the pointer associated with SPDY_set_cls_to_session().
880 *
881 * @param session handler to get its cls
882 * @return same pointer added by SPDY_set_cls_to_session() or
883 * NULL when nothing was associated
884 */
885void *
886SPDY_get_cls_from_session(struct SPDY_Session * session);
887
888
889/**
890 * Retrieves the remote address of a given session.
891 *
892 * @param session handler to get its remote address
893 * @param addr out parameter; pointing to remote address
894 * @return length of the address structure
895 */
896socklen_t
897SPDY_get_remote_addr(struct SPDY_Session * session,
898 struct sockaddr ** addr);
899
900
901/* SPDY name/value data structure handling functions */
902
903
904/**
905 * Create a new NameValue structure. It is needed for putting inside the
906 * HTTP headers and their values for a response. The user should later
907 * destroy alone the structure.
908 *
909 * @return hendler to the new empty structure or NULL on error
910 */
911struct SPDY_NameValue *
912SPDY_name_value_create ();
913
914
915/**
916 * Add name/value pair to a NameValue structure. SPDY_NO will be returned
917 * if the name/value pair is already in the structure. It is legal to
918 * add different values for the same name.
919 *
920 * @param container structure to which the new pair is added
921 * @param name for the value. Null-terminated string.
922 * @param value the value itself. Null-terminated string.
923 * @return SPDY_NO on error or SPDY_YES on success
924 */
925int
926SPDY_name_value_add (struct SPDY_NameValue * container,
927 const char * name,
928 const char * value);
929
930
931/**
932 * Lookup value for a name in a name/value structure.
933 *
934 * @param container structure in which to lookup
935 * @param name the name to look for
936 * @param num_values length of the returned array with values
937 * @return NULL if no such item was found, or an array containing the
938 * values
939 */
940const char * const *
941SPDY_name_value_lookup (struct SPDY_NameValue *container,
942 const char *name,
943 int * num_values);
944
945
946/**
947 * Iterate over name/value structure.
948 *
949 * @param container structure which to iterate over
950 * @param iterator callback to call on each name/value pair;
951 * maybe NULL (then just count headers)
952 * @param iterator_cls extra argument to iterator
953 * @return number of entries iterated over
954 */
955int
956SPDY_name_value_iterate (struct SPDY_NameValue *container,
957 SPDY_NameValueIterator iterator,
958 void *iterator_cls);
959
960
961/**
962 * Destroy a NameValue structure. Use this function to destroy only
963 * objects which, after passed to, will not be destroied by other
964 * functions.
965 *
966 */
967void
968SPDY_name_value_destroy (struct SPDY_NameValue * container);
969
970
971/* SPDY request handling functions */
972
973
974/**
975 * Gets the session responsible for the given
976 * request.
977 *
978 * @param request for which the session is wanted
979 * @return session handler for the request
980 */
981struct SPDY_Session *
982SPDY_get_session_for_request(const struct SPDY_Request * request);
983
984
985/**
986 * Associate a void pointer with a request. The data accessible by the
987 * pointer can later be used wherever the request handler is available.
988 *
989 * @param request with which to associate a pointer
990 * @param cls any data pointed by a pointer to be accessible later
991 */
992void
993SPDY_set_cls_to_request(struct SPDY_Request * request,
994 void * cls);
995
996
997/**
998 * Retrieves the pointer associated with the request by
999 * SPDY_set_cls_to_request().
1000 *
1001 * @param request to get its cls
1002 * @return same pointer added by SPDY_set_cls_to_request() or
1003 * NULL when nothing was associated
1004 */
1005void *
1006SPDY_get_cls_from_request(struct SPDY_Request * request);
1007
1008
1009/* SPDY response handling functions */
1010
1011
1012/**
1013 * Create response object containing all needed headers and data. The
1014 * response object is not bound to a request, so it can be used multiple
1015 * times with SPDY_queue_response() and schould be
1016 * destroied by calling the SPDY_destroy_response().<p>
1017 *
1018 * Currently the library does not provide compression of the body data.
1019 * It is up to the user to pass already compressed data and the
1020 * appropriate headers to this function when desired.
1021 *
1022 * @param status HTTP status code for the response (e.g. 404)
1023 * @param statustext HTTP status message for the response, which will
1024 * be appended to the status code (e.g. "OK"). Can be NULL
1025 * @param version HTTP version for the response (e.g. "http/1.1")
1026 * @param headers name/value structure containing additional HTTP headers.
1027 * Can be NULL. Can be used multiple times, it is up to
1028 * the user to destoy the object when not needed anymore.
1029 * @param data the body of the response. The lib will make a copy of it,
1030 * so it is up to the user to take care of the memory
1031 * pointed by data
1032 * @param size length of data. It can be 0, then the lib will send only
1033 * headers
1034 * @return NULL on error, handle to response object on success
1035 */
1036struct SPDY_Response *
1037SPDY_build_response(int status,
1038 const char * statustext,
1039 const char * version,
1040 struct SPDY_NameValue * headers,
1041 const void * data,
1042 size_t size);
1043
1044
1045/**
1046 * Create response object containing all needed headers. The data will
1047 * be provided later when the lib calls the callback function (just
1048 * before writing it to the TLS socket). The
1049 * response object is not bound to a request, so it can be used multiple
1050 * times with SPDY_queue_response() and schould be
1051 * destroied by calling the SPDY_destroy_response().<p>
1052 *
1053 * Currently the library does not provide compression of the body data.
1054 * It is up to the user to pass already compressed data and the
1055 * appropriate headers to this function and the callback when desired.
1056 *
1057 * @param status HTTP status code for the response (e.g. 404)
1058 * @param statustext HTTP status message for the response, which will
1059 * be appended to the status code (e.g. "OK"). Can be NULL
1060 * @param version HTTP version for the response (e.g. "http/1.1")
1061 * @param headers name/value structure containing additional HTTP headers.
1062 * Can be NULL. Can be used multiple times, it is up to
1063 * the user to destoy the object when not needed anymore.
1064 * @param rcb callback to use to obtain response data
1065 * @param rcb_cls extra argument to rcb
1066 * @param block_size preferred block size for querying rcb (advisory only,
1067 * the lib will call rcb specifying the block size); clients
1068 * should pick a value that is appropriate for IO and
1069 * memory performance requirements. The function will
1070 * fail if the value is bigger than the maximum
1071 * supported value (SPDY_MAX_SUPPORTED_FRAME_SIZE).
1072 * Can be 0, then the lib will use
1073 * SPDY_MAX_SUPPORTED_FRAME_SIZE instead.
1074 * @return NULL on error, handle to response object on success
1075 */
1076struct SPDY_Response *
1077SPDY_build_response_with_callback(int status,
1078 const char * statustext,
1079 const char * version,
1080 struct SPDY_NameValue * headers,
1081 SPDY_ResponseCallback rcb,
1082 void *rcb_cls,
1083 uint32_t block_size);
1084
1085
1086/**
1087 * Queue response object to be sent to the client. A successfully queued
1088 * response may never be sent, e.g. when the stream gets closed. The
1089 * data will be added to the output queue. The call will fail, if the
1090 * output for this session
1091 * is closed (i.e. the session is closed, half or full) or the output
1092 * channel for the stream, on which the request was received, is closed
1093 * (i.e. the stream is closed, half or full).
1094 *
1095 * @param request object identifying the request to which the
1096 * response is returned
1097 * @param response object containg headers and data to be sent
1098 * @param closestream TRUE if the server does NOT intend to PUSH
1099 * something more associated to this request/response later,
1100 * FALSE otherwise
1101 * @param consider_priority if FALSE, the response will be added to the
1102 * end of the queue. If TRUE, the response will be added after
1103 * the last previously added response with priority of the
1104 * request grater or equal to that of the current one. This
1105 * means that the function should be called with TRUE each time
1106 * if one wants to be sure that the output queue behaves like
1107 * a priority queue
1108 * @param rrcb callback called when all the data was sent (last frame
1109 * from response) or when that frame was discarded (e.g. the
1110 * stream has been closed meanwhile)
1111 * @param rrcb_cls extra argument to rcb
1112 * @return SPDY_NO on error or SPDY_YES on success
1113 */
1114int
1115SPDY_queue_response (struct SPDY_Request * request,
1116 struct SPDY_Response *response,
1117 bool closestream,
1118 bool consider_priority,
1119 SPDY_ResponseResultCallback rrcb,
1120 void * rrcb_cls);
1121
1122
1123/**
1124 * Destroy a response structure. It should be called for all objects
1125 * returned by SPDY_build_response*() functions to free the memory
1126 * associated with the prepared response. It is safe to call this
1127 * function not before being sure that the response will not be used by
1128 * the lib anymore, this means after SPDY_ResponseResultCallback
1129 * callbacks were called for all calls to SPDY_queue_response() passing
1130 * this response.
1131 *
1132 * @param response to destroy
1133 */
1134void
1135SPDY_destroy_response (struct SPDY_Response *response);
1136
1137
1138/* SPDY settings ID/value data structure handling functions */
1139
1140
1141/**
1142 * Create a new SettingsIDValue structure. It is needed for putting
1143 * inside tuples of SPDY option, flags and value for sending to the
1144 * client.
1145 *
1146 * @return hendler to the new empty structure or NULL on error
1147 */
1148const struct SPDY_Settings *
1149SPDY_settings_create ();
1150
1151
1152/**
1153 * Add or update a tuple to a SettingsIDValue structure.
1154 *
1155 * @param container structure to which the new tuple is added
1156 * @param id SPDY settings ID that will be sent. If this ID already in
1157 * container, the tupple for it will be updated (value and/or
1158 * flags). If it is not in the container, a new tupple will be
1159 * added.
1160 * @param flags SPDY settings flags applied only to this setting
1161 * @param value of the setting
1162 * @return SPDY_NO on error
1163 * or SPDY_YES if a new setting was added
1164 */
1165int
1166SPDY_settings_add (struct SPDY_Settings *container,
1167 enum SPDY_SETTINGS id,
1168 enum SPDY_FLAG_SETTINGS flags,
1169 int32_t value);
1170
1171
1172/**
1173 * Lookup value and flags for an ID in a settings ID/value structure.
1174 *
1175 * @param container structure in which to lookup
1176 * @param id SPDY settings ID to search for
1177 * @param flags out param for SPDY settings flags for this setting;
1178 * check it against the flags in enum SPDY_FLAG_SETTINGS
1179 * @param value out param for the value of this setting
1180 * @return SPDY_NO if the setting is not into the structure
1181 * or SPDY_YES if it is into it
1182 */
1183int
1184SPDY_settings_lookup (const struct SPDY_Settings * container,
1185 enum SPDY_SETTINGS id,
1186 enum SPDY_FLAG_SETTINGS * flags,
1187 int32_t * value);
1188
1189
1190/**
1191 * Iterate over settings ID/value structure.
1192 *
1193 * @param container structure which to iterate over
1194 * @param iterator callback to call on each ID/value pair;
1195 * maybe NULL (then just count number of settings)
1196 * @param iterator_cls extra argument to iterator
1197 * @return number of entries iterated over
1198 */
1199int
1200SPDY_settings_iterate (const struct SPDY_Settings * container,
1201 SPDY_SettingsIterator iterator,
1202 void * iterator_cls);
1203
1204
1205/**
1206 * Destroy a settings ID/value structure. Use this function to destroy
1207 * only objects which, after passed to, will not be destroied by other
1208 * functions.
1209 *
1210 * @param container structure which to detroy
1211 */
1212void
1213SPDY_settings_destroy (struct SPDY_Settings * container);
1214
1215
1216/* SPDY SETTINGS handling functions */
1217
1218
1219/**
1220 * Send SPDY SETTINGS to the client. The call will return fail if there
1221 * in invald setting into the settings container (e.g. invalid setting
1222 * ID).
1223 *
1224 * @param session SPDY_Session handler for which settings are being sent
1225 * @param settings ID/value pairs of the settings to be sent.
1226 * Can be used multiple times, it is up to the user to destoy
1227 * the object when not needed anymore.
1228 * @param flags for the whole settings frame. They are valid for all tuples
1229 * @param ... list of options (type-value pairs,
1230 * terminated with SPDY_SETTINGS_OPTION_END).
1231 * @return SPDY_NO on error or SPDY_YES on
1232 * success
1233 */
1234int
1235SPDY_send_settings (struct SPDY_Session * session,
1236 struct SPDY_Settings * settings,
1237 enum SPDY_FLAG_SETTINGS_FRAME flags,
1238 ...);
1239
1240
1241/* SPDY misc functions */
1242
1243
1244/**
1245 * Destroy a request structure. It should be called for all objects
1246 * received as a parameter in SPDY_NewRequestCallback to free the memory
1247 * associated with the request. It is safe to call this
1248 * function not before being sure that the request will not be used by
1249 * the lib anymore, this means after the stream, on which this request
1250 * had been sent, was closed and all SPDY_ResponseResultCallback
1251 * callbacks were called for all calls to SPDY_queue_response() passing
1252 * this request object.
1253 *
1254 * @param request to destroy
1255 */
1256void
1257SPDY_destroy_request (struct SPDY_Request * request);
1258
1259
1260/**
1261 * Send SPDY ping to the client
1262 *
1263 * @param session handler for which the ping request is sent
1264 * @param rttcb callback called when ping response to the request is
1265 * received
1266 * @param rttcb_cls extra argument to rttcb
1267 * @return SPDY_NO on error or SPDY_YES on success
1268 */
1269int
1270SPDY_send_ping(struct SPDY_Session * session,
1271 SPDY_PingCallback rttcb,
1272 void * rttcb_cls);
1273
1274
1275#endif