source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/contrib-2.1.0/examples/httpd/post_example/post_example.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 5.4 KB
Line 
1/**
2 * @file
3 * HTTPD example for simple POST
4 */
5
6 /*
7 * Copyright (c) 2017 Simon Goldschmidt
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Simon Goldschmidt <goldsimon@gmx.de>
35 *
36 */
37
38#include "lwip/opt.h"
39
40#include "lwip/apps/httpd.h"
41#include "lwip/def.h"
42#include "lwip/mem.h"
43
44#include <stdio.h>
45#include <string.h>
46
47/** define LWIP_HTTPD_EXAMPLE_GENERATEDFILES to 1 to enable this file system */
48#ifndef LWIP_HTTPD_EXAMPLE_SIMPLEPOST
49#define LWIP_HTTPD_EXAMPLE_SIMPLEPOST 0
50#endif
51
52#if LWIP_HTTPD_EXAMPLE_SIMPLEPOST
53
54#if !LWIP_HTTPD_SUPPORT_POST
55#error This needs LWIP_HTTPD_SUPPORT_POST
56#endif
57
58#define USER_PASS_BUFSIZE 16
59
60static void *current_connection;
61static void *valid_connection;
62static char last_user[USER_PASS_BUFSIZE];
63
64err_t
65httpd_post_begin(void *connection, const char *uri, const char *http_request,
66 u16_t http_request_len, int content_len, char *response_uri,
67 u16_t response_uri_len, u8_t *post_auto_wnd)
68{
69 LWIP_UNUSED_ARG(connection);
70 LWIP_UNUSED_ARG(http_request);
71 LWIP_UNUSED_ARG(http_request_len);
72 LWIP_UNUSED_ARG(content_len);
73 LWIP_UNUSED_ARG(post_auto_wnd);
74 if (!memcmp(uri, "/login.cgi", 11)) {
75 if (current_connection != connection) {
76 current_connection = connection;
77 valid_connection = NULL;
78 /* default page is "login failed" */
79 snprintf(response_uri, response_uri_len, "/loginfail.html");
80 /* e.g. for large uploads to slow flash over a fast connection, you should
81 manually update the rx window. That way, a sender can only send a full
82 tcp window at a time. If this is required, set 'post_aut_wnd' to 0.
83 We do not need to throttle upload speed here, so: */
84 *post_auto_wnd = 1;
85 return ERR_OK;
86 }
87 }
88 return ERR_VAL;
89}
90
91err_t
92httpd_post_receive_data(void *connection, struct pbuf *p)
93{
94 if (current_connection == connection) {
95 u16_t token_user = pbuf_memfind(p, "user=", 5, 0);
96 u16_t token_pass = pbuf_memfind(p, "pass=", 5, 0);
97 if ((token_user != 0xFFFF) && (token_pass != 0xFFFF)) {
98 u16_t value_user = token_user + 5;
99 u16_t value_pass = token_pass + 5;
100 u16_t len_user = 0;
101 u16_t len_pass = 0;
102 u16_t tmp;
103 /* find user len */
104 tmp = pbuf_memfind(p, "&", 1, value_user);
105 if (tmp != 0xFFFF) {
106 len_user = tmp - value_user;
107 } else {
108 len_user = p->tot_len - value_user;
109 }
110 /* find pass len */
111 tmp = pbuf_memfind(p, "&", 1, value_pass);
112 if (tmp != 0xFFFF) {
113 len_pass = tmp - value_pass;
114 } else {
115 len_pass = p->tot_len - value_pass;
116 }
117 if ((len_user > 0) && (len_user < USER_PASS_BUFSIZE) &&
118 (len_pass > 0) && (len_pass < USER_PASS_BUFSIZE)) {
119 /* provide contiguous storage if p is a chained pbuf */
120 char buf_user[USER_PASS_BUFSIZE];
121 char buf_pass[USER_PASS_BUFSIZE];
122 char *user = (char *)pbuf_get_contiguous(p, buf_user, sizeof(buf_user), len_user, value_user);
123 char *pass = (char *)pbuf_get_contiguous(p, buf_pass, sizeof(buf_pass), len_pass, value_pass);
124 if (user && pass) {
125 user[len_user] = 0;
126 pass[len_pass] = 0;
127 if (!strcmp(user, "lwip") && !strcmp(pass, "post")) {
128 /* user and password are correct, create a "session" */
129 valid_connection = connection;
130 memcpy(last_user, user, sizeof(last_user));
131 }
132 }
133 }
134 }
135 /* not returning ERR_OK aborts the connection, so return ERR_OK unless the
136 conenction is unknown */
137 return ERR_OK;
138 }
139 return ERR_VAL;
140}
141
142void
143httpd_post_finished(void *connection, char *response_uri, u16_t response_uri_len)
144{
145 /* default page is "login failed" */
146 snprintf(response_uri, response_uri_len, "/loginfail.html");
147 if (current_connection == connection) {
148 if (valid_connection == connection) {
149 /* login succeeded */
150 snprintf(response_uri, response_uri_len, "/session.html");
151 }
152 current_connection = NULL;
153 valid_connection = NULL;
154 }
155}
156
157#endif /* LWIP_HTTPD_EXAMPLE_SIMPLEPOST*/
Note: See TracBrowser for help on using the repository browser.