Beispiel #1
0
static void handle_call(struct mg_connection *nc, struct http_message *hm) {
  /* Send headers */
  mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");

  mg_send_http_chunk(nc, "It works!\r\n", 10);
  mg_send_http_chunk(nc, "", 0);
}
Beispiel #2
0
static void Http_write_data(struct v7 *v7, struct mg_connection *c) {
  v7_val_t arg0 = v7_arg(v7, 0);
  if (!v7_is_undefined(arg0)) {
    char buf[50], *p = buf;
    p = v7_stringify(v7, arg0, buf, sizeof(buf), 0);
    mg_send_http_chunk(c, p, strlen(p));
    if (p != buf) {
      free(p);
    }
  }
}
Beispiel #3
0
static void handle_get_cpu_usage(struct mg_connection *nc) {
  // Generate random value, as an example of changing CPU usage
  // Getting real CPU usage depends on the OS.
  int cpu_usage = (double) rand() / RAND_MAX * 100.0;

  // Use chunked encoding in order to avoid calculating Content-Length
  mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");

  // Output JSON object which holds CPU usage data
  mg_printf_http_chunk(nc, "{ \"result\": %d }", cpu_usage);

  // Send empty chunk, the end of response
  mg_send_http_chunk(nc, "", 0);
}
Beispiel #4
0
SJ_PRIVATE enum v7_err Http_response_end(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  DECLARE_CONN();

  rcode = Http_response_write(v7, res);
  if (rcode != V7_OK) {
    goto clean;
  }

  mg_send_http_chunk(c, "", 0);
  *res = v7_get_this(v7);

clean:
  return rcode;
}
Beispiel #5
0
static void handle_status_call(struct mg_connection *nc, struct http_message *hm) {
    int val;
    char *http_status;

    if ((val = usbfunk_init()) != USBFUNK_SUCCESS) {
        http_status = HTTP_503_SERVICE_UNAVAILABLE ;
    } else if ((val = usbfunk_status()) != USBFUNK_DEV_ERROR) {
        http_status = val ? HTTP_486_BUSY_HERE : HTTP_487_IDLE;
    } else {
        http_status = HTTP_400_BAD_REQUEST;
    }

    mg_printf(nc, "HTTP/1.1 %s\r\nTransfer-Encoding: chunked\r\n\r\n", http_status);
    mg_printf_http_chunk(nc, "%s", http_status);
    mg_send_http_chunk(nc, "", 0);
}
Beispiel #6
0
SJ_PRIVATE enum v7_err Http_request_end(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  DECLARE_CONN();

  rcode = Http_request_write(v7, res);
  if (rcode != V7_OK) {
    goto clean;
  }

  mg_send_http_chunk(c, "", 0);
  c->flags |= MG_F_CLOSE_CONNECTION_AFTER_RESPONSE;
  *res = v7_get_this(v7);

clean:
  return rcode;
}
Beispiel #7
0
static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) {
  char n1[100], n2[100];
  double result;

  /* Get form variables */
  mg_get_http_var(&hm->body, "n1", n1, sizeof(n1));
  mg_get_http_var(&hm->body, "n2", n2, sizeof(n2));

  /* Send headers */
  mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");

  /* Compute the result and send it back as a JSON object */
  result = strtod(n1, NULL) + strtod(n2, NULL);
  mg_printf_http_chunk(nc, "{ \"result\": %lf }", result);
  mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
}
Beispiel #8
0
static void handle_setled_call(struct mg_connection *nc, struct http_message *hm) {
    int val;
    char *http_status;

    char inverse[BUF_SIZE];
    mg_get_http_var(&hm->body, "inverse", inverse, sizeof(inverse));

    if ((val = usbfunk_init()) != USBFUNK_SUCCESS) {
        http_status = HTTP_503_SERVICE_UNAVAILABLE ;
    } else if ((val = usbfunk_setled(atoi(inverse)) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else {
        http_status = HTTP_400_BAD_REQUEST;
    }

    mg_printf(nc, "HTTP/1.1 %s\r\nTransfer-Encoding: chunked\r\n\r\n", http_status);
    mg_printf_http_chunk(nc, "%s", http_status);
    mg_send_http_chunk(nc, "", 0);
}
Beispiel #9
0
SJ_PRIVATE enum v7_err Http_get(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  rcode = sj_http_request_common(v7, v7_arg(v7, 0), v7_arg(v7, 1), res);
  if (rcode != V7_OK) {
    goto clean;
  }

  /* Prepare things to close the connection immediately after response */
  struct mg_connection *c = get_mgconn_obj(v7, *res);
  if (c == NULL) {
    rcode = v7_throwf(v7, "Error", "Connection is closed");
    goto clean;
  }

  mg_send_http_chunk(c, "", 0);
  c->flags |= MG_F_CLOSE_CONNECTION_AFTER_RESPONSE;

clean:
  return rcode;
}
Beispiel #10
0
static void handle_switch_call(struct mg_connection *nc, struct http_message *hm) {
    int val;
    char *http_status;

    char type[BUF_SIZE];
    char housecode[BUF_SIZE];
    char code[BUF_SIZE];
    char status[BUF_SIZE];
    char tone[BUF_SIZE];

    mg_get_http_var(&hm->body, "type", type, sizeof(type));
    mg_get_http_var(&hm->body, "housecode", housecode, sizeof(housecode));
    mg_get_http_var(&hm->body, "code", code, sizeof(code));
    mg_get_http_var(&hm->body, "status", status, sizeof(status));
    mg_get_http_var(&hm->body, "tone", tone, sizeof(tone));

    if ((val = usbfunk_init()) != USBFUNK_SUCCESS) {
        http_status = HTTP_503_SERVICE_UNAVAILABLE ;
    } else if (mg_casecmp(type, "pt0") == 0 && (val = usbfunk_switch_0(atoi(code)) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else if (mg_casecmp(type, "pt2") == 0 && (val = usbfunk_switch_2(atoi(housecode), code[0], mg_casecmp(status, "off") == 0 ? 0 : 1) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else if (mg_casecmp(type, "pt4") == 0 && (val = usbfunk_switch_4(atoi(housecode), code[0], mg_casecmp(status, "off") == 0 ? 0 : 1) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else if (mg_casecmp(type, "he") == 0 && (val = usbfunk_switch_he(atoi(code), mg_casecmp(status, "off") == 0 ? 0 : 1) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else if (mg_casecmp(type, "raw") == 0 && (val = usbfunk_switch_raw(code) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else if (mg_casecmp(type, "hx") == 0 && (val = usbfunk_bell(atoi(code), atoi(tone)) == USBFUNK_SUCCESS)) {
        http_status = HTTP_200_OK;
    } else {
        http_status = HTTP_400_BAD_REQUEST;
    }

    mg_printf(nc, "HTTP/1.1 %s\r\nTransfer-Encoding: chunked\r\n\r\n", http_status);
    mg_printf_http_chunk(nc, "%s", http_status);
    mg_send_http_chunk(nc, "", 0);
}
Beispiel #11
0
void Server::evHandler(struct mg_connection *nc, int ev, void *ev_data) {
	/**Se encarga de manejar los eventos generados en el server.**/
	if (!this->isSet()) {
		LOG(FATAL)<<"Server no esta setteado.";
	}
	struct http_message *hm = (struct http_message *) ev_data;
	shared_ptr<Server> server = Server::getServer();
	msg_t msg;
	switch (ev) {
		case MG_EV_HTTP_REQUEST:
				msg = server->handlerServ->handler(hm);
				LOG(INFO)<<"Mensaje: "<<(msg.body)<<","<<msg.status;
				mg_printf(nc, "HTTP/1.1 %d\r\n"
						"Transfer-Encoding: chunked\r\n"
						"\r\n", msg.status);
				mg_printf_http_chunk(nc, "%s", msg.body.c_str());
				mg_send_http_chunk(nc, "", 0);

				break;
		default:
			msg.status = BAD_REQUEST;
			break;
	}
}
Beispiel #12
0
static void handle_stop(struct mg_connection *con, struct http_message *msg) {
  mg_printf(con, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
  mg_send_http_chunk(con, "", 0);
  playback_stop = 1;
}
Beispiel #13
0
void WebServer::HTTPResponse::sendChunk(const char* pData, size_t length) {
	mg_send_http_chunk(m_nc, pData, length);
} // sendChunkHead