Exemplo n.º 1
0
void handle_read_call(struct mg_connection* nc, struct http_message* hm)
{
    char** result = init_result_array();
    char* tmp = init_tmp();
    if (result == NULL || tmp == NULL) {
        free(result);
        free(tmp);
        mg_error(nc, ERR_OUT_OF_MEMORY);
        return;
    }

    split(result, tmp, hm->query_string.p, "&=", hm->query_string.len);

    int resolution = -1;
    char* pict_id = NULL;
    parse_uri(result, &resolution, &pict_id);

    if (resolution != -1 && pict_id != NULL) {
        char* image_buffer = NULL;
        uint32_t image_size = 0;
        int err_check = do_read(pict_id, resolution, &image_buffer,
                                &image_size, db_file);
        if (err_check != 0) {
            mg_error(nc, err_check);
        } else {
            mg_printf(nc,
                      "HTTP/1.1 200 OK\r\n"
                      "Content-Type: image/jpeg\r\n"
                      "Content-Length: %" PRIu32 "\r\n\r\n",
                      image_size);
            mg_send(nc, image_buffer, image_size);
            nc->flags |= MG_F_SEND_AND_CLOSE;
        }
        free(image_buffer);
    } else {
        mg_error(nc, ERR_INVALID_ARGUMENT);
    }

    free(result);
    free(tmp);
}
Exemplo n.º 2
0
void handle_delete_call(struct mg_connection* nc,
                        struct http_message* hm)
{
    char** result = init_result_array();
    char* tmp = init_tmp();
    if (result == NULL || tmp == NULL) {
        free(result);
        free(tmp);
        mg_error(nc, ERR_OUT_OF_MEMORY);
        return;
    }

    split(result, tmp, hm->query_string.p, "&=", hm->query_string.len);

    int err_check = -1;
    char* pict_id = NULL;
    parse_uri(result, &err_check, &pict_id);

    if (pict_id != NULL) {
        err_check = do_delete(db_file, pict_id);
        if (err_check == 0) {
            mg_printf(nc,
                      "HTTP/1.1 302 Found\r\n"
                      "Location: http://localhost:%s/index.html\r\n",
                      s_http_port);
            nc->flags |= MG_F_SEND_AND_CLOSE;
        }
    }

    if (err_check != 0) {
        mg_error(nc, err_check);
    }

    free(result);
    free(tmp);
}
Exemplo n.º 3
0
void handle_list_call(struct mg_connection* nc)
{
    char* json_list = do_list(db_file, JSON);
    if (json_list == NULL) {
        mg_error(nc, ERR_IO);
        return;
    }
    size_t msg_length = strlen(json_list);
    mg_printf(nc,
              "HTTP/1.1 200 OK\r\n"
              "Content-Type: application/json\r\n"
              "Content-Length: %zu\r\n\r\n%s",
              msg_length, json_list);
    nc->flags |= MG_F_SEND_AND_CLOSE;
    free(json_list);
}
Exemplo n.º 4
0
static void mg_join(struct mg_connection* conn, const struct mg_request_info* ri) {
	if(strcmp(ri->request_method, "GET") == 0) {
		uint new_id = rand_uint();

		if(!aatree_insert(&clients, new_id, (void*)1))
			goto error;

		mg_printf(conn, "%s", standard_reply);
		mg_printf(conn, "%u\n", new_id);

		// Invoke join_cb
		_invoke("join_cb", new_id, NULL);

		return;
	}
	
error:
	mg_error(conn, ri);
}
Exemplo n.º 5
0
void handle_insert_call(struct mg_connection* nc,
                        struct http_message* hm)
{
    int err_check = db_file->header.num_files < db_file->header.max_files ? 0 :
                    ERR_FULL_DATABASE;

    if (err_check == 0) {
        char var_name[100];
        char file_name[MAX_PIC_ID + 1];
        const char* chunk;
        size_t chunk_len = 0;
        size_t n1 = 0;
        size_t n2 = 0;

        while ((n2 = mg_parse_multipart(hm->body.p + n1,
                                        hm->body.len - n1,
                                        var_name, sizeof(var_name),
                                        file_name, sizeof(file_name),
                                        &chunk, &chunk_len)) > 0) {
            printf("var: %s, file_name: %s, size: %zu, chunk: [%.*s]\n",
                   var_name, file_name, chunk_len, (int) chunk_len, chunk);
            n1 += n2;
        }

        err_check = do_insert(chunk, chunk_len, file_name, db_file);

        // Success
        if (err_check == 0) {
            mg_printf(nc,
                      "HTTP/1.1 302 Found\r\n"
                      "Location: http://localhost:%s/index.html\r\n",
                      s_http_port);
            nc->flags |= MG_F_SEND_AND_CLOSE;
        }
    }

    if (err_check != 0) {
        mg_error(nc, err_check);
    }
}
Exemplo n.º 6
0
static void mg_leave(struct mg_connection* conn, const struct mg_request_info* ri) {
	const char *cl;
	char *buf;
	int len;

	if (strcmp(ri->request_method, "POST") == 0 &&
		(cl = mg_get_header(conn, "Content-Length")) != NULL) {
		len = atoi(cl);
		if ((buf = MEM_ALLOC(len+1)) != NULL) {
			mg_read(conn, buf, len);
			buf[len] = '\0';

			uint id;
			sscanf(buf, "%u", &id); 
			MEM_FREE(buf);

			if(aatree_size(&clients)) {
				aatree_remove(&clients, id);

				// Invoke leave_cb
				_invoke("leave_cb", id, NULL);

				mg_printf(conn, "%s", standard_reply);
				return;
			}
			else {
				goto error;
			}
		}
	}
	else if(strcmp(ri->request_method, "OPTIONS") == 0) {
		mg_printf(conn, "%s", standard_reply);
		return;
	}

error:
	mg_error(conn, ri);
}