example_httpuplds.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #include <limits.h>
  30. #include <sagui.h>
  31. /* NOTE: Error checking has been omitted to make it clear. */
  32. #ifdef _WIN32
  33. #define PATH_SEP '\\'
  34. #else /* _WIN32 */
  35. #define PATH_SEP '/'
  36. #endif /* _WIN32 */
  37. #define PAGE_FORM \
  38. "<html>" \
  39. "<body>" \
  40. "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">" \
  41. "<fieldset>" \
  42. "<legend>Choose the files:</legend>" \
  43. "File 1: <input type=\"file\" name=\"file1\"/><br>" \
  44. "File 2: <input type=\"file\" name=\"file2\"/><br>" \
  45. "<input type=\"submit\"/>" \
  46. "</fieldset>" \
  47. "</form>" \
  48. "</body>" \
  49. "</html>"
  50. #define PAGE_DONE \
  51. "<html>" \
  52. "<head>" \
  53. "<title>Uploads</title>" \
  54. "</head>" \
  55. "<body>" \
  56. "<strong>Uploaded files:</strong><br>" \
  57. "%s" \
  58. "</body>" \
  59. "</html>"
  60. #define CONTENT_TYPE "text/html; charset=utf-8"
  61. static void process_uploads(struct sg_httpreq *req, struct sg_httpres *res) {
  62. struct sg_httpupld *upld;
  63. struct sg_str *body;
  64. const char *name;
  65. char *str;
  66. char errmsg[256];
  67. int errnum;
  68. body = sg_str_new();
  69. sg_str_printf(body, "<ol>");
  70. upld = sg_httpreq_uploads(req);
  71. while (upld) {
  72. name = sg_httpupld_name(upld);
  73. errnum = sg_httpupld_save(upld, true);
  74. if (errnum == 0)
  75. sg_str_printf(body, "<li><a href=\"?file=%s\">%s</a></li>", name, name);
  76. else {
  77. sg_strerror(errnum, errmsg, sizeof(errmsg));
  78. sg_str_printf(body, "<li><font color='red'>%s - failed - %s</font></li>",
  79. name, errmsg);
  80. }
  81. sg_httpuplds_next(&upld);
  82. }
  83. sg_str_printf(body, "</ol>");
  84. str = strdup(sg_str_content(body));
  85. sg_str_clear(body);
  86. sg_str_printf(body, PAGE_DONE, str);
  87. free(str);
  88. sg_httpres_send(res, sg_str_content(body), CONTENT_TYPE, 200);
  89. sg_str_free(body);
  90. }
  91. static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req,
  92. struct sg_httpres *res) {
  93. struct sg_strmap **qs;
  94. const char *file;
  95. char path[PATH_MAX];
  96. if (sg_httpreq_is_uploading(req))
  97. process_uploads(req, res);
  98. else {
  99. qs = sg_httpreq_params(req);
  100. if (qs && sg_strmap_count(*qs) > 0) {
  101. file = sg_strmap_get(*qs, "file");
  102. if (file) {
  103. sprintf(path, "%s%c%s", sg_httpsrv_upld_dir(sg_httpreq_srv(req)),
  104. PATH_SEP, file);
  105. sg_httpres_download(res, path, 200);
  106. }
  107. } else
  108. sg_httpres_send(res, PAGE_FORM, CONTENT_TYPE, 200);
  109. }
  110. }
  111. int main(int argc, const char *argv[]) {
  112. struct sg_httpsrv *srv;
  113. uint16_t port;
  114. if (argc != 2) {
  115. printf("%s <PORT>\n", argv[0]);
  116. return EXIT_FAILURE;
  117. }
  118. port = strtol(argv[1], NULL, 10);
  119. srv = sg_httpsrv_new(req_cb, NULL);
  120. if (!sg_httpsrv_listen(srv, port, false)) {
  121. sg_httpsrv_free(srv);
  122. return EXIT_FAILURE;
  123. }
  124. fprintf(stdout, "Server running at http://localhost:%d\n",
  125. sg_httpsrv_port(srv));
  126. fflush(stdout);
  127. getchar();
  128. sg_httpsrv_free(srv);
  129. return EXIT_SUCCESS;
  130. }