embed.c 5.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2004-2009 Sergey Lyubka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Unit test for the mongoose web server. Tests embedded API.
22 23 24 25 26


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
27 28 29 30
#ifndef _WIN32
#include <unistd.h>
#endif

31 32 33
#include "mongoose.h"

#if !defined(LISTENING_PORT)
34
#define LISTENING_PORT "23456"
35
#endif
36

37
static const char *standard_reply = "HTTP/1.1 200 OK\r\n"
38 39
  "Content-Type: text/plain\r\n"
  "Connection: close\r\n\r\n";
40

41 42
static void test_get_var(struct mg_connection *conn,
                         const struct mg_request_info *ri) {
43
  char *var, *buf;
44 45 46
  size_t buf_len;
  const char *cl;
  int var_len;
47

48
  mg_printf(conn, "%s", standard_reply);
49

50 51 52 53
  buf_len = 0;
  var = buf = NULL;
  cl = mg_get_header(conn, "Content-Length");
  mg_printf(conn, "cl: %p\n", cl);
54 55
  if ((!strcmp(ri->request_method, "POST") ||
       !strcmp(ri->request_method, "PUT"))
56
      && cl != NULL) {
57 58
    buf_len = atoi(cl);
    buf = malloc(buf_len);
59 60 61 62 63 64 65
    /* Read in two pieces, to test continuation */
    if (buf_len > 2) {
      mg_read(conn, buf, 2);
      mg_read(conn, buf + 2, buf_len - 2);
    } else {
      mg_read(conn, buf, buf_len);
    }
66 67 68 69 70 71
  } else if (ri->query_string != NULL) {
    buf_len = strlen(ri->query_string);
    buf = malloc(buf_len + 1);
    strcpy(buf, ri->query_string);
  }
  var = malloc(buf_len + 1);
72
  var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1);
73 74 75 76
  mg_printf(conn, "Value: [%s]\n", var);
  mg_printf(conn, "Value size: [%d]\n", var_len);
  free(buf);
  free(var);
77 78
}

79 80
static void test_get_header(struct mg_connection *conn,
                            const struct mg_request_info *ri) {
81 82
  const char *value;
  int i;
83

84
  mg_printf(conn, "%s", standard_reply);
85 86 87 88
  printf("HTTP headers: %d\n", ri->num_headers);
  for (i = 0; i < ri->num_headers; i++) {
    printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value);
  }
89

90 91 92
  value = mg_get_header(conn, "Host");
  if (value != NULL) {
    mg_printf(conn, "Value: [%s]", value);
93
  }
94 95
}

96 97
static void test_get_request_info(struct mg_connection *conn,
                                  const struct mg_request_info *ri) {
98
  int i;
99

100
  mg_printf(conn, "%s", standard_reply);
101

102 103 104
  mg_printf(conn, "Method: [%s]\n", ri->request_method);
  mg_printf(conn, "URI: [%s]\n", ri->uri);
  mg_printf(conn, "HTTP version: [%s]\n", ri->http_version);
105

106 107 108 109
  for (i = 0; i < ri->num_headers; i++) {
    mg_printf(conn, "HTTP header [%s]: [%s]\n",
              ri->http_headers[i].name,
              ri->http_headers[i].value);
110
  }
111

112 113 114 115 116 117
  mg_printf(conn, "Query string: [%s]\n",
            ri->query_string ? ri->query_string: "");
  mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip);
  mg_printf(conn, "Remote port: [%d]\n", ri->remote_port);
  mg_printf(conn, "Remote user: [%s]\n",
            ri->remote_user ? ri->remote_user : "");
118 119
}

120 121
static void test_error(struct mg_connection *conn,
                       const struct mg_request_info *ri) {
122
  int status = (int) ri->ev_data;
123
  mg_printf(conn, "HTTP/1.1 %d XX\r\n"
124 125
            "Conntection: close\r\n\r\n", status);
  mg_printf(conn, "Error: [%d]", status);
126 127
}

128 129 130 131 132
static void test_post(struct mg_connection *conn,
                      const struct mg_request_info *ri) {
  const char *cl;
  char *buf;
  int len;
133

134
  mg_printf(conn, "%s", standard_reply);
135 136 137 138 139 140 141 142
  if (strcmp(ri->request_method, "POST") == 0 &&
      (cl = mg_get_header(conn, "Content-Length")) != NULL) {
    len = atoi(cl);
    if ((buf = malloc(len)) != NULL) {
      mg_write(conn, buf, len);
      free(buf);
    }
  }
143 144
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158
static const struct test_config {
  enum mg_event event;
  const char *uri;
  void (*func)(struct mg_connection *, const struct mg_request_info *);
} test_config[] = {
  {MG_NEW_REQUEST, "/test_get_header", &test_get_header},
  {MG_NEW_REQUEST, "/test_get_var", &test_get_var},
  {MG_NEW_REQUEST, "/test_get_request_info", &test_get_request_info},
  {MG_NEW_REQUEST, "/test_post", &test_post},
  {MG_HTTP_ERROR, "", &test_error},
  {0, NULL, NULL}
};

static void *callback(enum mg_event event,
159 160
                      struct mg_connection *conn) {
  const struct mg_request_info *request_info = mg_get_request_info(conn);
161 162 163 164 165 166 167 168 169 170 171 172
  int i;

  for (i = 0; test_config[i].uri != NULL; i++) {
    if (event == test_config[i].event &&
        (event == MG_HTTP_ERROR ||
         !strcmp(request_info->uri, test_config[i].uri))) {
      test_config[i].func(conn, request_info);
      return "processed";
    }
  }

  return NULL;
173 174
}

175
int main(void) {
176
  struct mg_context *ctx;
177
  const char *options[] = {"listening_ports", LISTENING_PORT, NULL};
178

179
  ctx = mg_start(callback, NULL, options);
180 181
  pause();
  return 0;
182
}