Bladeren bron

Use MG_FORM_FIELD_STORAGE_GET in embedded_c example

bel2125 6 jaren geleden
bovenliggende
commit
124b236121
1 gewijzigde bestanden met toevoegingen van 47 en 8 verwijderingen
  1. 47 8
      examples/embedded_c/embedded_c.c

+ 47 - 8
examples/embedded_c/embedded_c.c

@@ -23,7 +23,7 @@
 
 
 
 
 #define DOCUMENT_ROOT "."
 #define DOCUMENT_ROOT "."
-#ifdef NO_SSL
+#ifndef TEST_WITHOUT_SSL
 #ifdef USE_IPV6
 #ifdef USE_IPV6
 #define PORT "[::]:8888,8884"
 #define PORT "[::]:8888,8884"
 #else
 #else
@@ -223,6 +223,24 @@ FileHandler(struct mg_connection *conn, void *cbdata)
 }
 }
 
 
 
 
+#define MD5_STATIC static
+#include "../src/md5.inl"
+
+/* Stringify binary data. Output buffer must be twice as big as input,
+ * because each byte takes 2 bytes in string representation */
+static void
+bin2str(char *to, const unsigned char *p, size_t len)
+{
+	static const char *hex = "0123456789abcdef";
+
+	for (; len--; p++) {
+		*to++ = hex[p[0] >> 4];
+		*to++ = hex[p[0] & 0x0f];
+	}
+	*to = '\0';
+}
+
+
 int
 int
 field_found(const char *key,
 field_found(const char *key,
             const char *filename,
             const char *filename,
@@ -240,7 +258,7 @@ field_found(const char *key,
 #else
 #else
 		snprintf(path, pathlen, "/tmp/%s", filename);
 		snprintf(path, pathlen, "/tmp/%s", filename);
 #endif
 #endif
-		return MG_FORM_FIELD_STORAGE_STORE;
+		return MG_FORM_FIELD_STORAGE_GET;
 	}
 	}
 	return MG_FORM_FIELD_STORAGE_GET;
 	return MG_FORM_FIELD_STORAGE_GET;
 }
 }
@@ -251,10 +269,33 @@ field_get(const char *key, const char *value, size_t valuelen, void *user_data)
 {
 {
 	struct mg_connection *conn = (struct mg_connection *)user_data;
 	struct mg_connection *conn = (struct mg_connection *)user_data;
 
 
-	if (key[0]) {
-		mg_printf(conn, "%s = ", key);
+	if ((key != NULL) && (key[0] == '\0')) {
+		/* Incorrect form data detected */
+		return MG_FORM_FIELD_HANDLE_ABORT;
+	}
+	if ((valuelen > 0) && (value == NULL)) {
+		/* Unreachable, since this call will not be generated by civetweb. */
+		return MG_FORM_FIELD_HANDLE_ABORT;
+	}
+
+	if (key) {
+		mg_printf(conn, "key = %s\n", key);
+	}
+	mg_printf(conn, "valuelen = %u\n", valuelen);
+
+	if (valuelen > 0) {
+		/* mg_write(conn, value, valuelen); */
+
+		md5_byte_t hash[16];
+		md5_state_t ctx;
+		char outputbuf[33];
+
+		md5_init(&ctx);
+		md5_append(&ctx, (const md5_byte_t *)value, valuelen);
+		md5_finish(&ctx, hash);
+		bin2str(outputbuf, hash, sizeof(hash));
+		mg_printf(conn, "value md5 hash = %s\n", outputbuf);
 	}
 	}
-	mg_write(conn, value, valuelen);
 
 
 	return 0;
 	return 0;
 }
 }
@@ -322,8 +363,6 @@ FileUploadForm(struct mg_connection *conn, void *cbdata)
 	return 1;
 	return 1;
 }
 }
 
 
-#define MD5_STATIC static
-#include "../src/md5.inl"
 
 
 struct tfile_checksum {
 struct tfile_checksum {
 	char name[128];
 	char name[128];
@@ -925,7 +964,7 @@ main(int argc, char *argv[])
 	int port_cnt, n;
 	int port_cnt, n;
 	int err = 0;
 	int err = 0;
 
 
-/* Check if libcivetweb has been built with all required features. */
+	/* Check if libcivetweb has been built with all required features. */
 #ifdef USE_IPV6
 #ifdef USE_IPV6
 	if (!mg_check_feature(8)) {
 	if (!mg_check_feature(8)) {
 		fprintf(stderr,
 		fprintf(stderr,