Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
add http_reply_json
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredh committed Aug 6, 2022
1 parent 10bb7b5 commit 023a32e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* HTTP
*/

int http_reply_json(struct http_conn *conn, const char *sessid,
const struct odict *od);
int http_reply_descr(struct http_conn *conn, const char *sessid,
enum sdp_type type, struct mbuf *mb_sdp);

Expand Down
46 changes: 31 additions & 15 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@
#include "demo.h"


int http_reply_json(struct http_conn *conn, const char *sessid,
const struct odict *od)
{
const char *ctype = "application/json";
char *buf = NULL;
int err;

if (!conn)
return EINVAL;

err = re_sdprintf(&buf, "%H", json_encode_odict, od);
if (err)
goto out;

err = http_reply(conn, 201, "Created",
"Content-Type: %s\r\n"
"Content-Length: %zu\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Session-ID: %s\r\n"
"\r\n"
"%s",
ctype, str_len(buf), sessid, buf);

out:
mem_deref(buf);

return err;
}


/*
* format:
*
Expand All @@ -27,9 +57,7 @@
int http_reply_descr(struct http_conn *conn, const char *sessid,
enum sdp_type type, struct mbuf *mb_sdp)
{
const char *ctype = "application/json";
struct odict *od = NULL;
char *buf = NULL;
int err;

if (!conn || !mb_sdp)
Expand All @@ -39,23 +67,11 @@ int http_reply_descr(struct http_conn *conn, const char *sessid,
if (err)
goto out;

err = re_sdprintf(&buf, "%H", json_encode_odict, od);
err = http_reply_json(conn, sessid, od);
if (err)
goto out;

info("demo: reply: %s\n", ctype);

err = http_reply(conn, 201, "Created",
"Content-Type: %s\r\n"
"Content-Length: %zu\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Session-ID: %s\r\n"
"\r\n"
"%s",
ctype, str_len(buf), sessid, buf);

out:
mem_deref(buf);
mem_deref(od);

return err;
Expand Down

0 comments on commit 023a32e

Please sign in to comment.