plugins add win modifiers before libwebsockets.h for dll export flag
[platform/upstream/libwebsockets.git] / plugins / protocol_post_demo.c
1 /*
2  * ws protocol handler plugin for "POST demo"
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
21 #define LWS_DLL
22 #define LWS_INTERNAL
23 #include "../lib/libwebsockets.h"
24
25 #include <string.h>
26
27 struct per_session_data__post_demo {
28         char post_string[256];
29         char result[500 + LWS_PRE];
30         int result_len;
31 };
32
33 static int
34 callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
35                    void *user, void *in, size_t len)
36 {
37         struct per_session_data__post_demo *pss =
38                         (struct per_session_data__post_demo *)user;
39         unsigned char buffer[LWS_PRE + 512];
40         unsigned char *p, *start, *end;
41         int n;
42
43         switch (reason) {
44
45         case LWS_CALLBACK_HTTP_BODY:
46                 lwsl_debug("LWS_CALLBACK_HTTP_BODY: len %d\n", (int)len);
47                 strncpy(pss->post_string, in, sizeof (pss->post_string) -1);
48                 pss->post_string[sizeof(pss->post_string) - 1] = '\0';
49
50                 if (len < sizeof(pss->post_string) - 1)
51                         pss->post_string[len] = '\0';
52                 break;
53
54         case LWS_CALLBACK_HTTP_WRITEABLE:
55                 lwsl_debug("LWS_CALLBACK_HTTP_WRITEABLE: sending %d\n", pss->result_len);
56                 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
57                               pss->result_len, LWS_WRITE_HTTP);
58                 if (n < 0)
59                         return 1;
60                 goto try_to_reuse;
61
62         case LWS_CALLBACK_HTTP_BODY_COMPLETION:
63                 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
64                 /*
65                  * the whole of the sent body arrived,
66                  * respond to the client with a redirect to show the
67                  * results
68                  */
69                 pss->result_len = sprintf((char *)pss->result + LWS_PRE,
70                             "<html><body><h1>Form results</h1>'%s'<br>"
71                             "</body></html>", pss->post_string);
72
73                 p = buffer + LWS_PRE;
74                 start = p;
75                 end = p + sizeof(buffer) - LWS_PRE;
76
77                 if (lws_add_http_header_status(wsi, 200, &p, end))
78                         return 1;
79
80                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
81                                 (unsigned char *)"text/html", 9, &p, end))
82                         return 1;
83                 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
84                         return 1;
85                 if (lws_finalize_http_header(wsi, &p, end))
86                         return 1;
87
88                 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
89                 if (n < 0)
90                         return 1;
91
92                 /*
93                  *  send the payload next time, in case would block after
94                  * headers
95                  */
96                 lws_callback_on_writable(wsi);
97                 break;
98
99         default:
100                 break;
101         }
102
103         return 0;
104
105 try_to_reuse:
106         if (lws_http_transaction_completed(wsi))
107                 return -1;
108
109         return 0;
110 }
111
112 static const struct lws_protocols protocols[] = {
113         {
114                 "protocol-post-demo",
115                 callback_post_demo,
116                 sizeof(struct per_session_data__post_demo),
117                 1024,
118         },
119 };
120
121 LWS_EXTERN LWS_VISIBLE int
122 init_protocol_post_demo(struct lws_context *context,
123                         struct lws_plugin_capability *c)
124 {
125         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
126                 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
127                          c->api_magic);
128                 return 1;
129         }
130
131         c->protocols = protocols;
132         c->count_protocols = ARRAY_SIZE(protocols);
133         c->extensions = NULL;
134         c->count_extensions = 0;
135
136         return 0;
137 }
138
139 LWS_EXTERN LWS_VISIBLE int
140 destroy_protocol_post_demo(struct lws_context *context)
141 {
142         return 0;
143 }