This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
fcgi.vapi
563 lines (524 loc) · 17.3 KB
/
fcgi.vapi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
* This FastCGI application library source and object code (the
* "Software") and its documentation (the "Documentation") are
* copyrighted by Open Market, Inc ("Open Market"). The following terms
* apply to all files associated with the Software and Documentation
* unless explicitly disclaimed in individual files.
*
* Open Market permits you to use, copy, modify, distribute, and license
* this Software and the Documentation for any purpose, provided that
* existing copyright notices are retained in all copies and that this
* notice is included verbatim in any distributions. No written
* agreement, license, or royalty fee is required for any of the
* authorized uses. Modifications to this Software and Documentation may
* be copyrighted by their authors and need not follow the licensing
* terms described here. If modifications to this Software and
* Documentation have new licensing terms, the new terms must be clearly
* indicated on the first page of each file where they apply.
*
* OPEN MARKET MAKES NO EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE
* SOFTWARE OR THE DOCUMENTATION, INCLUDING WITHOUT LIMITATION ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN
* NO EVENT SHALL OPEN MARKET BE LIABLE TO YOU OR ANY THIRD PARTY FOR ANY
* DAMAGES ARISING FROM OR RELATING TO THIS SOFTWARE OR THE
* DOCUMENTATION, INCLUDING, WITHOUT LIMITATION, ANY INDIRECT, SPECIAL OR
* CONSEQUENTIAL DAMAGES OR SIMILAR DAMAGES, INCLUDING LOST PROFITS OR
* LOST DATA, EVEN IF OPEN MARKET HAS BEEN ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGES. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS".
* OPEN MARKET HAS NO LIABILITY IN CONTRACT, TORT, NEGLIGENCE OR
* OTHERWISE ARISING OUT OF THIS SOFTWARE OR THE DOCUMENTATION.
*/
/**
* FastCGI server interface library
*
* This library makes it easy to be a server for FastCGI programs so that a web
* server, like Apache, nginx, or lighthttpd can proxy for this application.
*
* There are three interfaces: a CGI-esque interface available through
* {@link stdin} and friends, a single-threaded interface available via
* {@link accept} and a multi-threaded interface available via {@link request}.
*/
[CCode (cheader_filename = "fcgiapp.h")]
namespace FastCGI {
/**
* Listening socket file number
*/
[CCode (cheader_filename = "fastcgi.h", cname = "FCGI_LISTENSOCK_FILENO")]
public const int LISTENSOCK_FILENO;
/**
* Values for role component of FCGI_BeginRequestBody
*/
[CCode (cheader_filename = "fastcgi.h", cprefix = "FCGI_")]
public enum Role {
RESPONDER,
AUTHORIZER,
FILTER
}
/**
* FastCGI's abstraction over a file I/O
*/
[CCode (cname = "FCGI_FILE", cheader_filename = "fcgi_stdio.h", free_function = "FCGI_pclose", has_type_id = false)]
public class FileStream {
public int descriptor {
[CCode (cname = "FCGI_fileno")]
get;
}
public GLib.FileStream file_stream {
[CCode (cname = "FCGI_ToFILE")]
get;
}
public Stream stream {
[CCode (cname = "FCGI_ToFcgiStream")]
get;
}
[CCode (cname = "FCGI_fopen")]
public static FileStream? open (string path, string mode);
[CCode (cname = "FCGI_fdopen")]
public static FileStream? fdopen (int fildes, string mode);
[CCode (cname = "FCGI_tmpfile")]
public static FileStream? tmpfile ();
[CCode (cname = "FCGI_popen")]
public static FileStream? popen (string cmd, string type);
[ReturnsModifiedPointer]
[CCode (cname = "FCGI_freopen", instance_pos = -1)]
public static FileStream? reopen (string path, string mode);
[CCode (cname = "FCGI_clearerr")]
public void clear_error ();
[CCode (cname = "FCGI_feof")]
public bool eof ();
[CCode (cname = "FCGI_fflush")]
public int flush ();
[CCode (cname = "FCGI_fgetc")]
public int getc ();
[CCode (cname = "FCGI_fgets", instance_pos = -1)]
public unowned string? gets (char[] s);
[CCode (cname = "FCGI_ferror")]
public int get_error ();
[CCode (cname = "FCGI_fprintf")]
[PrintfFormat ()]
public void printf (string format, ...);
[CCode (cname = "FCGI_fputc", instance_pos = -1)]
public void putc (char c);
[CCode (cname = "FCGI_fputs", instance_pos = -1)]
public void puts (string s);
[CCode (cname = "FCGI_fread", instance_pos = -1)]
public size_t read ([CCode (array_length_pos = 2.1)] uint8[] buf, size_t size = 1);
[CCode (cname = "FCGI_rewind")]
public void rewind ();
[CCode (cname = "FCGI_fseek")]
public int seek (long offset, GLib.FileSeek whence);
[CCode (cname = "FCGI_ftell")]
public int tell ();
[CCode (cname = "FCGI_ungetc", instance_pos = -1)]
public int ungetc (int c);
[CCode (cname = "FCGI_vfprintf")]
public void vprintf (string format, va_list args);
[CCode (cname = "FCGI_fwrite", instance_pos = -1)]
public size_t write ([CCode (array_length_pos = 2.1)] uint8[] buf, size_t size = 1);
}
/**
* The state of a FastCGI stream.
*
* Streams are modeled after the {@link GLib.FileStream}. (We wouldn't need
* our own if platform vendors provided a standard way to subclass theirs.)
* The state of a stream is private and should only be accessed by the
* procedures defined below.
*/
[CCode (cname = "FCGX_Stream", free_function = "FCGX_FClose", has_type_id = false)]
[Compact]
public class Stream {
[CCode (cname = "isReader")]
public bool is_reader;
[CCode (cname = "isClosed")]
public bool is_closed;
/**
* Create a stream (used by cgi-fcgi).
*
* This shouldn't be needed by a FastCGI application.
*/
[CCode (cname = "FCGX_CreateWriter")]
public static Stream create_writer (int socket, int request_id, int bufflen, int streamType);
/**
* Clear the stream error code and end-of-file indication.
*/
[CCode (cname = "FCGX_ClearError")]
public void clear_error ();
/**
* Flushes any buffered output.
*
* Server-push is a legitimate application of this method. Otherwise,
* this method is not very useful, since {@link accept} does it
* implicitly. Calling it in non-push applications results in extra
* writes and therefore reduces performance.
*/
[CCode (cname = "FCGX_FFlush")]
public bool flush ();
/**
* Closes the stream. For writers, flushes any buffered output.
*
* Close is not a very useful operation since FCGX_Accept does it
* implicitly. Closing the out stream before the err stream results in
* an extra write if there's nothing in the err stream, and therefore
* reduces performance.
*
* @return EOF (-1) if an error occurred.
*/
[CCode (cname = "FCGX_FClose")]
public int close ();
/**
* Return the stream error code.
* @return 0 means no error, > 0 is an errno(2) error, < 0 is an FastCGI error.
*/
[CCode (cname = "FCGX_GetError")]
public int get_error ();
/**
* Reads a byte from the input stream and returns it.
*
* @return The byte, or {@link GLib.FileStream.EOF} if the end of input has been reached.
*/
[CCode (cname = "FCGX_GetChar")]
public int getc ();
[CCode (cname = "FCGX_GetLine", instance_pos = 1.2)]
public unowned string? gets (uint8[] buffer);
/**
* Returns true if end-of-file has been detected while reading from
* stream.
*
* Note that this may return false, yet an immediately following
* {@link getc} may return EOF. This function, like the standard C stdio
* function {@link GLib.FileStream.eof}, does not provide the ability to
* peek ahead.
*/
[CCode (cname = "FCGX_HasSeenEOF")]
public bool has_seen_eof ();
/**
* Performs printf-style output formatting and writes the results to the
* output stream.
*
* @return Number of bytes written for normal return, or {@link GLib.FileStream.EOF}
* if an error occurred.
*/
[CCode (cname = "FCGX_FPrintF")]
[PrintfFormat]
public int printf (string format, ...);
/**
* Writes the buffer into the output stream.
*
* Performs no interpretation of the output bytes.
*
* @return Number of bytes written for normal return, or {@link GLib.FileStream.EOF}
* if an error occurred.
*/
[CCode (cname = "FCGX_PutStr", instance_pos = 1.3)]
public int put_str (uint8[] buffer);
/**
* Writes a byte to the output stream.
*
* @return The byte, or {@link GLib.FileStream.EOF} if an error occurred.
*/
[CCode (cname = "FCGX_PutChar", instance_pos = 1.2)]
public int putc (int c);
/**
* Writes a string to the output stream.
* @return Number of bytes written for normal return, or {@link GLib.FileStream.EOF}
* if an error occurred.
*/
[CCode (cname = "FCGX_PutS", instance_pos = -1)]
public int puts (string str);
/**
* Reads up to consecutive bytes from the input stream
* into the character array.
*
* Performs no interpretation of the input bytes.
* @return Number of bytes read. If result is smaller than the buffer size,
* end of input has been reached.
*/
[CCode (cname = "FCGX_GetStr", instance_pos = 1.2)]
public int read (uint8[] buffer);
/**
* Sets the exit status for stream's request.
*
* The exit status is the status code the request would have exited
* with, had the request been run as a CGI program. You can call this
* several times during a request; the last call before the request ends
* determines the value.
*/
[CCode (cname = "FCGX_SetExitStatus", instance_pos = 1.2)]
public void set_exit_status (int status);
/**
* Repositions an input stream to the start of FCGI_DATA.
*
* If the preconditions are not met sets the stream error code to
* {@link CALL_SEQ_ERROR}.
*
* @return 0 for a normal return, < 0 for error
*/
[CCode (cname = "FCGX_StartFilterData")]
public int start_filter ();
/**
* Pushes back the character onto the input stream.
*
* One character of pushback is guaranteed once a character
* has been read. No pushback is possible for EOF.
*
* @return c if the pushback succeeded, {@link GLib.FileStream.EOF} if not.
*/
[CCode (cname = "FCGX_UnGetChar", instance_pos = 1.2)]
public int ungetc (int c);
[CCode (cname = "FCGX_VFPrintF")]
public int vprintf (string format, va_list arg);
}
/**
* CGI parameters
*/
[CCode (cname = "FCGX_ParamArray", has_type_id = false)]
[SimpleType]
public struct parameters {
[CCode (cname = "FCGX_GetParam", instance_pos = -1)]
public unowned string? get (string name);
[CCode (array_null_terminated = true, array_length = false)]
public unowned string[] get_all () {
return (string[]) this;
}
}
/**
* State associated with a request.
*
* This is the API for multi-threaded code. Code should be structured like
* this:
*
* {{{
* int descriptor;
* Mutex mutex;
* void handler() {
* FastCGI.request request;
* assert(FastCGI.request.init(out request, descriptor) == 0);
* while (true) {
* mutex.lock () ;
* var fail = request.accept() < 0;
* mutex.unlock();
* if (fail)
* break;
* // Process request
* request.finish();
* }
* request.close(false);
* }
* void main() {
* mutex = new Mutex();
* assert(FastCGI.init() == 0);
* stderr.printf("I:open_socket path=\"%s\" backlog=%d\n", socket_path, backlog);
* descriptor = FastCGI.open_socket(socket_path, backlog);
* assert(descriptor != -1);
* try {
* while (thread_count > 1) {
* Thread.create<void>(handler, false);
* thread_count--;
* }
* handler();
* } catch(ThreadError e) {
* // Handle error
* }
* }
* }}}
*
* It is important that only one thread at a time attempt to call
* {@link accept}, hence the mutex. The request can be read from the client
* using {@link environment} and the response can be written to the client
* using {@link out}.
*/
[CCode (cname = "FCGX_Request", has_type_id = false, destroy_function = "")]
public struct request {
[CCode (cname = "envp")]
public parameters environment;
public Stream err;
public Stream @in;
public Stream @out;
[CCode (cname = "requestId")]
public int request_id;
public Role role;
/* Don't use anything below here */
[CCode (cname = "ipcFd")]
public int ipc_fd;
[CCode (cname = "isBeginProcessed")]
public bool is_begin_processed;
[CCode (cname = "keepConnection")]
public bool keep_connection;
[CCode (cname = "appStatus")]
public int app_status;
[CCode (cname = "nWriters")]
public int number_of_writers;
public int flags;
[CCode (cname = "listen_sock")]
public int listen_socket;
/**
* Accept a new request
*
* Thread-safe.
*
* Finishes the request accepted by (and frees any storage allocated by)
* the previous call. Creates input, output, and error streams and
* assigns them to in, out, and err respectively. Creates a parameters
* data structure.
*
* DO NOT retain pointers to the envp or any strings contained in it,
* since these will be freed by the next call or a call to {@link finish}.
*
* @return 0 for successful call, -1 for error.
*/
[CCode (cname = "FCGX_Accept_r")]
public int accept ();
/**
* Close this stream.
* @param close_descriptor Close the underlying file descriptor.
*/
[CCode (cname = "FCGX_Free")]
[DestroysInstance]
public void close (bool close_descriptor = true);
/**
* Finish the request
*
* Thread-safe.
*
* Finishes the request accepted by (and frees any storage allocated by)
* the previous call to {@link accept}.
*
* DO NOT retain pointers to the envp array or any strings contained in
* it, since these will be freed.
*/
[CCode (cname = "FCGX_Finish_r")]
public void finish ();
/**
* Initialize a request.
*
* @param sock is a file descriptor returned by {@link open_socket} or 0 (default).
*
* @return 0 upon success.
*/
[CCode (cname = "FCGX_InitRequest")]
public static int init (out request request, int sock = 0, RequestFlags flags = RequestFlags.NONE);
/**
* Get a named parameter from the environment.
*/
public unowned string? @get (string name) {
return environment[name];
}
}
[CCode (cname = "int", cprefix = "FCGI_", has_type_id = false)]
[Flags]
public enum RequestFlags {
[CCode (cname = "0")]
NONE,
/**
* Do not restart upon being interrupted.
*/
FAIL_ACCEPT_ON_INTR
}
/**
* Accept a new request
*
* NOT thread-safe.
*
* Finishes the request accepted by (and frees any storage allocated by) the
* previous call. Creates input, output, and error streams and assigns them
* to in, out, and err respectively. Creates a parameters data structure
* and assigns it to envp.
*
* DO NOT retain pointers to the envp array or any strings contained in it,
* since these will be freed by the next call or a call to {@link finish}.
*
* Code is generally of this form:
*
* {{{
* Stream @in;
* Stream @out;
* Stream @err;
* unowned parameters envp;
* while(accept(out @in, out @out, out @err, out envp) == 0) {
* // Process request, writing output to @out.
* finish();
* }
* }}}
*
* @return 0 for successful call, -1 for error.
*/
[CCode (cname = "FCGX_Accept")]
public int accept (out Stream @in, out Stream @out, out Stream err, out unowned parameters envp);
/**
* Finish the current request
*
* NOT thread-safe.
*
* Finishes the request accepted by (and frees any storage allocated by) the
* previous call to {@link accept}.
*
* DO NOT retain pointers to the {@link parameters} or any strings contained
* in it, since these will be freed.
*
* @see accept
*/
[CCode (cname = "FCGX_Finish")]
public void finish ();
/**
* Initialize the FCGX library.
*
* Call in multi-threaded apps.
* @return 0 upon success.
*/
[CCode (cname = "FCGX_Init")]
public int init ();
/**
* Is this process a CGI process rather than a FastCGI process.
*/
[CCode (cname = "FCGX_IsCGI")]
public bool is_cgi ();
/**
* Create a FastCGI listen socket.
*
* @param path is the Unix domain socket (named pipe for WinNT), or a colon
* followed by a port number. (e.g. "/tmp/fastcgi/mysocket", ":5000")
* @param backlog is the listen queue depth used in the listen() call.
* @return the socket's file descriptor or -1 on error.
*/
[CCode (cname = "FCGX_OpenSocket")]
public int open_socket (string path, int backlog);
/**
* Prevent the lib from accepting any new requests.
*
* Signal handler safe.
*/
[CCode (cname = "FCGX_ShutdownPending")]
public void shutdown_pending ();
/**
* Standard error abstraction using FastCGI.
*
* This should be used if you are not using the multi-threaded {@link accept}
* or {@link request}-based API.
*/
[CCode (cname = "FCGI_stderr", cheader_filename = "fcgi_stdio.h")]
public static FileStream stderr;
/**
* Standard input abstraction using FastCGI.
*
* This should be used if you are not using the multi-threaded {@link accept}
* or {@link request}-based API.
*/
[CCode (cname = "FCGI_stdin", cheader_filename = "fcgi_stdio.h")]
public static FileStream stdin;
/**
* Standard output abstraction using FastCGI.
*
* This should be used if you are not using the multi-threaded {@link accept}
* or {@link request}-based API.
*/
[CCode (cname = "FCGI_stdout", cheader_filename = "fcgi_stdio.h")]
public static FileStream stdout;
[CCode (cname = "FCGX_CALL_SEQ_ERROR")]
public const int CALL_SEQ_ERROR;
[CCode (cname = "FCGX_PARAMS_ERROR")]
public const int PARAMS_ERROR;
[CCode (cname = "FCGX_PROTOCOL_ERROR")]
public const int PROTOCOL_ERROR;
[CCode (cname = "FCGX_UNSUPPORTED_VERSION")]
public const int UNSUPPORTED_VERSION;
}