• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes connection definition for RESTful HTTP server.
32  */
33 
34 #ifndef OTBR_REST_CONNECTION_HPP_
35 #define OTBR_REST_CONNECTION_HPP_
36 
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "common/mainloop.hpp"
41 #include "rest/parser.hpp"
42 #include "rest/resource.hpp"
43 
44 using std::chrono::steady_clock;
45 
46 namespace otbr {
47 namespace rest {
48 
49 /**
50  * This class implements a Connection class of each socket connection.
51  *
52  */
53 class Connection : public MainloopProcessor
54 {
55 public:
56     /**
57      * The constructor is to initialize a socket connection instance.
58      *
59      * @param[in] aStartTime  The reference start time of a connection which
60      *                        is set when created for the first time and maybe
61      *                        reset when transfer to wait callback or wait write
62      *                        state.
63      * @param[in] aResource   A pointer to the resource handler.
64      * @param[in] aFd         The file descriptor for the connection.
65      *
66      */
67     Connection(steady_clock::time_point aStartTime, Resource *aResource, int aFd);
68 
69     /**
70      * The desctructor destroys the connection instance.
71      *
72      */
73     ~Connection(void) override;
74 
75     /**
76      * This method initializes the connection.
77      *
78      *
79      */
80     void Init(void);
81 
82     void Update(MainloopContext &aMainloop) override;
83     void Process(const MainloopContext &aMainloop) override;
84 
85     /**
86      * This method indicates whether this connection no longer need to be processed.
87      *
88      * @retval TRUE   This connection could be released in next loop.
89      * @retval FALSE  This connection still needs to be processed in next loop.
90      *
91      */
92     bool IsComplete(void) const;
93 
94 private:
95     void UpdateReadFdSet(fd_set &aReadFdSet, int &aMaxFd) const;
96     void UpdateWriteFdSet(fd_set &aWriteFdSet, int &aMaxFd) const;
97     void UpdateTimeout(timeval &aTimeout) const;
98     void ProcessWaitRead(const fd_set &aReadFdSet);
99     void ProcessWaitCallback(void);
100     void ProcessWaitWrite(const fd_set &aWriteFdSet);
101     void Write(void);
102     void Handle(void);
103     void Disconnect(void);
104 
105     // Timestamp used for each check point of a connection
106     steady_clock::time_point mTimeStamp;
107 
108     // File descriptor for this connection
109     int mFd;
110 
111     // Enum indicates the state of this connection
112     ConnectionState mState;
113 
114     // Response instance binded to this connection
115     Response mResponse;
116 
117     // Request instance binded to this connection
118     Request mRequest;
119 
120     // HTTP parser instance
121     Parser mParser;
122 
123     // Resource handler instance
124     Resource *mResource;
125 
126     // Write buffer in case write multiple times
127     std::string mWriteContent;
128 };
129 
130 } // namespace rest
131 } // namespace otbr
132 
133 #endif // OTBR_REST_CONNECTION_HPP_
134