Preface

cgull is a way to write Cloudflare Workers in C. You write an ordinary C program that registers a few routes and handles requests; cgull compiles it to WebAssembly and wraps it in the few lines of JavaScript a Worker needs. You don't write the JavaScript, and you need not know it is there.

Here is a complete program:

#include <cgull.h>

static void hello(cg_req *req, cg_res *res)
{
    const char *name = cg_param(req, "name");

    cg_printf(res, "<h1>Hello, %s!</h1>", cg_esc(name));
}

int main(void)
{
    cg_get("/hello/:name", hello);
    return cg_run();
}

Deployed, it answers /hello/world with a greeting, from whichever of Cloudflare's data centres is nearest the visitor. The program is the one you see: there is no event loop to start and no server to configure. Each request runs the program from the top of main, in a fresh instance with its own memory. When the response has been sent, the instance is thrown away, and with it everything the program allocated.

That makes C an easier language for this than you might expect. You never call free, because nothing outlives the request. A crash, even a bad pointer, spoils only the request that caused it, and the visitor sees a 500. And the program is, underneath, a plain CGI program of the kind written in the 1990s, so it builds natively too and can be run under gdb or the sanitizers.

The name is a C program that lives at the edge, like the bird; the cg of its prefix, as in cg_get, is for CGI, which is what it is underneath. Say it "seagull".

This book is in the spirit of the one whose cover it borrows. Chapter 1 is a tutorial that gets a Worker running and deployed. Chapter 2 covers the cgull command. Chapters 3 to 7 take the library a topic at a time: routing, requests, responses, memory, and what the program can see of its surroundings. Chapter 8 explains how it all works, which is short, because there isn't much of it. Appendix A is the reference manual, with every name in <cgull.h>.

The book is itself a cgull program. Its chapters are HTML files compiled into the program with C23's #embed; the C code numbers the sections, works out the page numbers in the contents and the index, links every function name to its entry in the reference manual, and answers the search box.

Contents

  1. Prefaceix
  2. Chapter 1A Tutorial Introduction1
  3. 1.1 Getting Started1
  4. 1.2 The First Program1
  5. 1.3 Running It1
  6. 1.4 Parameters2
  7. 1.5 Forms2
  8. 1.6 Deploying3
  9. Chapter 2The cgull Command5
  10. 2.1 cgull new5
  11. 2.2 cgull build5
  12. 2.3 cgull run6
  13. 2.4 cgull dev and deploy6
  14. 2.5 cgull setup7
  15. 2.6 The Project7
  16. 2.7 Compiler Flags7
  17. Chapter 3Routing9
  18. 3.1 Routes9
  19. 3.2 Patterns9
  20. 3.3 Methods10
  21. 3.4 Middleware10
  22. 3.5 Not Found11
  23. Chapter 4Requests13
  24. 4.1 Method and Path13
  25. 4.2 The Query String13
  26. 4.3 Headers13
  27. 4.4 Cookies13
  28. 4.5 Bodies and Forms13
  29. Chapter 5Responses15
  30. 5.1 Status and Headers15
  31. 5.2 Writing the Body15
  32. 5.3 Shortcuts15
  33. 5.4 Redirects16
  34. 5.5 Cookies16
  35. 5.6 When Things Go Wrong16
  36. Chapter 6Strings and Memory17
  37. 6.1 One Request, One Instance17
  38. 6.2 Never Free17
  39. 6.3 Escaping17
  40. 6.4 Building Strings18
  41. 6.5 Limits18
  42. Chapter 7The Environment19
  43. 7.1 Variables and Secrets19
  44. 7.2 Logging19
  45. 7.3 Static Files19
  46. 7.4 Time and Chance19
  47. 7.5 What Isn't There20
  48. Chapter 8How cgull Works21
  49. 8.1 CGI21
  50. 8.2 The Request Going In21
  51. 8.3 The Shim22
  52. 8.4 Native Builds22
  53. 8.5 What Comes Next23
  54. Appendix AReference Manual25
  55. A.1 Types and Macros25
  56. A.2 Routing25
  57. A.3 The Request26
  58. A.4 The Response27
  59. A.5 Strings28
  60. A.6 The Environment29
  61. Index31

Set in Tinos and Courier Prime. Served by a C program on Cloudflare Workers, cgull 0.1.0.