Browse Source

Simplify REST example (remove HTTPS)

The REST example can be built with and without HTTPS, but the HTTPS build depends on a prepared
Linux build environment. The HTTPS adds useless complexity here: We don't want to show Linux build
environments, but only give a short example for REST.
bel2125 3 years ago
parent
commit
ba650fc1ab
2 changed files with 9 additions and 54 deletions
  1. 4 4
      examples/rest/Makefile
  2. 5 50
      examples/rest/rest.c

+ 4 - 4
examples/rest/Makefile

@@ -1,4 +1,4 @@
-# 
+#
 # Copyright (c) 2018 CivetWeb Developers
 # Copyright (c) 2013 No Face Press, LLC
 # License http://opensource.org/licenses/mit-license.php MIT License
@@ -18,17 +18,17 @@ LIBS = -lpthread
 
 include $(TOP)/resources/Makefile.in-os
 
-ifeq ($(TARGET_OS),LINUX) 
+ifeq ($(TARGET_OS),LINUX)
 	LIBS += -ldl
 endif
 
 all: $(PROG)
 
 $(PROG): $(CIVETWEB_LIB) $(SRC)
-	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) -lcrypto -lssl -DUSE_SSL_DH=1
+	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS)
 
 $(CIVETWEB_LIB):
-	$(MAKE) -C $(TOP) WITH_IPV6=1 WITH_WEBSOCKET=1 COPT='-DNO_SSL_DL=1' clean lib
+	$(MAKE) -C $(TOP) WITH_IPV6=1 WITH_WEBSOCKET=1 COPT='-DNO_SSL' clean lib
 	cp $(TOP)/$(CIVETWEB_LIB) .
 
 clean:

+ 5 - 50
examples/rest/rest.c

@@ -17,14 +17,8 @@
 #include "cJSON.h"
 #include "civetweb.h"
 
-
-#ifdef NO_SSL
 #define PORT "8089"
 #define HOST_INFO "http://localhost:8089"
-#else
-#define PORT "8089r,8843s"
-#define HOST_INFO "https://localhost:8843"
-#endif
 
 #define EXAMPLE_URI "/example"
 #define EXIT_URI "/exit"
@@ -65,7 +59,6 @@ ExampleGET(struct mg_connection *conn)
 		return 500;
 	}
 
-
 	cJSON_AddStringToObject(obj, "version", CIVETWEB_VERSION);
 	cJSON_AddNumberToObject(obj, "request", ++request);
 	SendJSON(conn, obj);
@@ -140,22 +133,6 @@ ExamplePUT(struct mg_connection *conn)
 
 
 static int
-ExamplePOST(struct mg_connection *conn)
-{
-	/* In this example, do the same for PUT and POST */
-	return ExamplePUT(conn);
-}
-
-
-static int
-ExamplePATCH(struct mg_connection *conn)
-{
-	/* In this example, do the same for PUT and PATCH */
-	return ExamplePUT(conn);
-}
-
-
-static int
 ExampleHandler(struct mg_connection *conn, void *cbdata)
 {
 
@@ -165,18 +142,15 @@ ExampleHandler(struct mg_connection *conn, void *cbdata)
 	if (0 == strcmp(ri->request_method, "GET")) {
 		return ExampleGET(conn);
 	}
-	if (0 == strcmp(ri->request_method, "PUT")) {
+	if ((0 == strcmp(ri->request_method, "PUT"))
+	    || (0 == strcmp(ri->request_method, "POST"))
+	    || (0 == strcmp(ri->request_method, "PATCH"))) {
+		/* In this example, do the same for PUT, POST and PATCH */
 		return ExamplePUT(conn);
 	}
-	if (0 == strcmp(ri->request_method, "POST")) {
-		return ExamplePOST(conn);
-	}
 	if (0 == strcmp(ri->request_method, "DELETE")) {
 		return ExampleDELETE(conn);
 	}
-	if (0 == strcmp(ri->request_method, "PATCH")) {
-		return ExamplePATCH(conn);
-	}
 
 	/* this is not a GET request */
 	mg_send_http_error(
@@ -231,28 +205,9 @@ main(int argc, char *argv[])
 	struct mg_context *ctx;
 	int err = 0;
 
-/* Check if libcivetweb has been built with all required features. */
-#ifndef NO_SSL
-	if (!mg_check_feature(2)) {
-		fprintf(stderr,
-		        "Error: Embedded example built with SSL support, "
-		        "but civetweb library build without.\n");
-		err = 1;
-	}
-
-
-	mg_init_library(MG_FEATURES_SSL);
-
-#else
+	/* Check if libcivetweb has been built with all required features. */
 	mg_init_library(0);
 
-#endif
-	if (err) {
-		fprintf(stderr, "Cannot start CivetWeb - inconsistent build.\n");
-		return EXIT_FAILURE;
-	}
-
-
 	/* Callback will print error messages to console */
 	memset(&callbacks, 0, sizeof(callbacks));
 	callbacks.log_message = log_message;