add protocol plugin for post demo
[platform/upstream/libwebsockets.git] / plugins / protocol_post_demo.c
1 /*
2  * ws protocol handler plugin for "dumb increment"
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * These test plugins are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20 #include "../lib/libwebsockets.h"
21 #include <string.h>
22
23 struct per_session_data__post_demo {
24         char post_string[256];
25         char result[500 + LWS_PRE];
26         int result_len;
27 };
28
29 static int
30 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
31                    void *user, void *in, size_t len)
32 {
33         struct per_session_data__post_demo *pss =
34                         (struct per_session_data__post_demo *)user;
35         unsigned char buffer[LWS_PRE + 512];
36         unsigned char *p, *start, *end;
37         int n;
38
39         switch (reason) {
40         case LWS_CALLBACK_HTTP:
41                 lwsl_debug("LWS_CALLBACK_HTTP\n");
42                 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
43                         return 0;
44                 break;
45
46         case LWS_CALLBACK_HTTP_BODY:
47                 lwsl_debug("LWS_CALLBACK_HTTP_BODY: len %d\n", (int)len);
48                 strncpy(pss->post_string, in, sizeof (pss->post_string) -1);
49                 pss->post_string[sizeof(pss->post_string) - 1] = '\0';
50
51                 if (len < sizeof(pss->post_string) - 1)
52                         pss->post_string[len] = '\0';
53                 break;
54
55         case LWS_CALLBACK_HTTP_WRITEABLE:
56                 lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n", pss->result_len);
57                 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
58                               pss->result_len, LWS_WRITE_HTTP);
59                 if (n < 0)
60                         return 1;
61                 goto try_to_reuse;
62
63         case LWS_CALLBACK_HTTP_BODY_COMPLETION:
64                 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
65                 /*
66                  * the whole of the sent body arrived,
67                  * respond to the client with a redirect to show the
68                  * results
69                  */
70                 pss->result_len = sprintf((char *)pss->result + LWS_PRE,
71                             "<html><body><h1>Form results</h1>'%s'<br>"
72                             "</body></html>", pss->post_string);
73
74                 p = buffer + LWS_PRE;
75                 start = p;
76                 end = p + sizeof(buffer) - LWS_PRE;
77
78                 if (lws_add_http_header_status(wsi, 200, &p, end))
79                         return 1;
80
81                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
82                                 (unsigned char *)"text/html", 9, &p, end))
83                         return 1;
84                 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
85                         return 1;
86                 if (lws_finalize_http_header(wsi, &p, end))
87                         return 1;
88
89                 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
90                 if (n < 0)
91                         return 1;
92
93                 /*
94                  *  send the payload next time, in case would block after
95                  * headers
96                  */
97                 lws_callback_on_writable(wsi);
98                 break;
99
100         default:
101                 break;
102         }
103
104         return 0;
105
106 try_to_reuse:
107         if (lws_http_transaction_completed(wsi))
108                 return -1;
109
110         return 0;
111 }
112
113 static const struct lws_protocols protocols[] = {
114         {
115                 "protocol-post-demo",
116                 callback_post_demo,
117                 sizeof(struct per_session_data__post_demo),
118                 1024,
119         },
120 };
121
122 LWS_VISIBLE int
123 init_protocol_post_demo(struct lws_context *context,
124                         struct lws_plugin_capability *c)
125 {
126         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
127                 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
128                          c->api_magic);
129                 return 1;
130         }
131
132         c->protocols = protocols;
133         c->count_protocols = ARRAY_SIZE(protocols);
134         c->extensions = NULL;
135         c->count_extensions = 0;
136
137         return 0;
138 }
139
140 LWS_VISIBLE int
141 destroy_protocol_post_demo(struct lws_context *context)
142 {
143         return 0;
144 }