libafcgi is a libev based asynchronous FastCGI library https://redmine.lighttpd.net/projects/libafcgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

168 lines
4.4 KiB

  1. #ifndef _FCGI_CGI_FASTCGI_H
  2. #define _FCGI_CGI_FASTCGI_H
  3. #include "libafcgi-config.h"
  4. /* NO multiplexing support */
  5. #include <glib.h>
  6. #include <ev.h>
  7. /* FastCGI constants */
  8. # define FCGI_VERSION_1 1
  9. # define FCGI_HEADER_LEN 8
  10. enum FCGI_Type {
  11. FCGI_BEGIN_REQUEST = 1,
  12. FCGI_ABORT_REQUEST = 2,
  13. FCGI_END_REQUEST = 3,
  14. FCGI_PARAMS = 4,
  15. FCGI_STDIN = 5,
  16. FCGI_STDOUT = 6,
  17. FCGI_STDERR = 7,
  18. FCGI_DATA = 8,
  19. FCGI_GET_VALUES = 9,
  20. FCGI_GET_VALUES_RESULT = 10,
  21. FCGI_UNKNOWN_TYPE = 11
  22. };
  23. # define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
  24. enum FCGI_Flags {
  25. FCGI_KEEP_CONN = 1
  26. };
  27. enum FCGI_Role {
  28. FCGI_RESPONDER = 1,
  29. FCGI_AUTHORIZER = 2,
  30. FCGI_FILTER = 3
  31. };
  32. enum FCGI_ProtocolStatus {
  33. FCGI_REQUEST_COMPLETE = 0,
  34. FCGI_CANT_MPX_CONN = 1,
  35. FCGI_OVERLOADED = 2,
  36. FCGI_UNKNOWN_ROLE = 3
  37. };
  38. #define FASTCGI_MAX_KEYLEN 1024
  39. #define FASTCGI_MAX_VALUELEN 64*1024
  40. /* end FastCGI constants */
  41. struct fastcgi_server;
  42. typedef struct fastcgi_server fastcgi_server;
  43. struct fastcgi_callbacks;
  44. typedef struct fastcgi_callbacks fastcgi_callbacks;
  45. struct fastcgi_connection;
  46. typedef struct fastcgi_connection fastcgi_connection;
  47. struct fastcgi_queue;
  48. typedef struct fastcgi_queue fastcgi_queue;
  49. struct fastcgi_server {
  50. /* custom user data */
  51. gpointer data;
  52. /* private data */
  53. gboolean do_shutdown;
  54. const fastcgi_callbacks *callbacks;
  55. guint max_connections;
  56. GPtrArray *connections;
  57. guint cur_requests;
  58. gint fd;
  59. struct ev_loop *loop;
  60. ev_io fd_watcher;
  61. ev_prepare closing_watcher;
  62. };
  63. struct fastcgi_callbacks {
  64. void (*cb_new_connection)(fastcgi_connection *fcon); /* new connection accepted */
  65. void (*cb_new_request)(fastcgi_connection *fcon); /* new request on connection, env/params are ready */
  66. void (*cb_wrote_data)(fastcgi_connection *fcon);
  67. void (*cb_received_stdin)(fastcgi_connection *fcon, GByteArray *data); /* data == NULL => eof */
  68. void (*cb_received_data)(fastcgi_connection *fcon, GByteArray *data); /* data == NULL => eof */
  69. void (*cb_request_aborted)(fastcgi_connection *fcon);
  70. void (*cb_reset_connection)(fastcgi_connection *fcon); /* cleanup custom data before fcon is freed, not for keep-alive */
  71. };
  72. struct fastcgi_queue {
  73. GQueue queue;
  74. gsize offset; /* offset in first chunk */
  75. gsize length;
  76. gboolean closed;
  77. };
  78. struct fastcgi_connection {
  79. /* custom user data */
  80. gpointer data;
  81. /* read/write */
  82. GHashTable *environ; /* GString -> GString */
  83. /* read only */
  84. fastcgi_server *fsrv;
  85. guint fcon_id; /* index in server con array */
  86. gboolean closing; /* "dead" connection */
  87. /* current request */
  88. guint16 requestID;
  89. guint16 role;
  90. guint8 flags;
  91. /* private data */
  92. unsigned char headerbuf[8];
  93. guint headerbuf_used;
  94. gboolean first;
  95. struct {
  96. guint8 version;
  97. guint8 type;
  98. guint16 requestID;
  99. guint16 contentLength;
  100. guint8 paddingLength;
  101. } current_header;
  102. guint content_remaining, padding_remaining;
  103. GByteArray *buffer, *parambuf;
  104. gint fd;
  105. ev_io fd_watcher;
  106. gboolean read_suspended;
  107. /* write queue */
  108. fastcgi_queue write_queue;
  109. };
  110. fastcgi_server *fastcgi_server_create(struct ev_loop *loop, gint socketfd, const fastcgi_callbacks *callbacks, guint max_connections);
  111. void fastcgi_server_stop(fastcgi_server *fsrv); /* stop accepting new connections, closes listening socket */
  112. void fastcgi_server_free(fastcgi_server *fsrv);
  113. void fastcgi_suspend_read(fastcgi_connection *fcon);
  114. void fastcgi_resume_read(fastcgi_connection *fcon);
  115. void fastcgi_end_request(fastcgi_connection *fcon, gint32 appStatus, enum FCGI_ProtocolStatus status);
  116. void fastcgi_send_out(fastcgi_connection *fcon, GString *data);
  117. void fastcgi_send_err(fastcgi_connection *fcon, GString *data);
  118. void fastcgi_send_out_bytearray(fastcgi_connection *fcon, GByteArray *data);
  119. void fastcgi_send_err_bytearray(fastcgi_connection *fcon, GByteArray *data);
  120. void fastcgi_connection_close(fastcgi_connection *fcon); /* shouldn't be needed */
  121. void fastcgi_queue_append_string(fastcgi_queue *queue, GString *buf);
  122. void fastcgi_queue_append_bytearray(fastcgi_queue *queue, GByteArray *buf);
  123. void fastcgi_queue_clear(fastcgi_queue *queue);
  124. /* return values: 0 ok, -1 error, -2 con closed */
  125. gint fastcgi_queue_write(int fd, fastcgi_queue *queue, gsize max_write);
  126. char** fastcgi_build_env(fastcgi_connection *con);
  127. const gchar* fastcgi_connection_environ_lookup(fastcgi_connection *fcon, const gchar* key, gsize keylen);
  128. #endif