Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

66 wiersze
2.4 KiB

  1. #define DJSTD_BASIC_ENTRY
  2. #include "core.c"
  3. int djstd_entry(Arena *arena, StringList args) {
  4. Socket sock = socketConnect(arena, (SocketConnectInfo){ .address="::1", .port=8080, .blocking=true });
  5. string newLine = s("\r\n");
  6. string lines[] = {
  7. s("GET / HTTP/1.1"),
  8. s("Host: localhost"),
  9. };
  10. for (EachInArray(lines, i)) {
  11. socketWriteStr(&sock, lines[i]);
  12. socketWriteStr(&sock, newLine);
  13. }
  14. socketWriteStr(&sock, newLine);
  15. CharList body = EmptyList();
  16. bool streamingBody = false;
  17. Forever {
  18. StringResult response = socketReadStr(arena, &sock);
  19. if (response.valid) {
  20. if (streamingBody) {
  21. if (body.capacity - body.length >= response.result.length) {
  22. memcpy(body.data + body.length, response.result.str, response.result.length);
  23. body.length += response.result.length;
  24. }
  25. } else {
  26. StringList lines = strSplit(arena, s("\r\n"), response.result);
  27. for (EachEl(lines, string, line)) {
  28. if (body.capacity > 0 && strEql(*line, s(""))) {
  29. streamingBody = true;
  30. } else if (streamingBody && (body.capacity - body.length) >= line->length) {
  31. // TODO(dledda): append list to list, string to string, whatever
  32. // TODO(dledda): `joinStringList`
  33. memcpy(body.data + body.length, line->str, line->length);
  34. body.length += line->length;
  35. } else {
  36. // TODO(dledda): `strContains`
  37. // TODO(dledda): `stringContains` -> `strContainsChar`
  38. StringList split = strSplit(arena, s("Content-Length: "), *line);
  39. if (split.length > 1) {
  40. Int32Result lengthResult = parsePositiveInt(split.data[1]);
  41. if (lengthResult.valid) {
  42. body = PushList(arena, CharList, lengthResult.result);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. if (streamingBody == true && body.length == body.capacity) {
  50. break;
  51. }
  52. }
  53. // TODO(dledda): string from CharList/ByteList
  54. // TODO(dledda): `printStr`
  55. print("%S", (string){.str=body.data, .length=body.length});
  56. return 0;
  57. }