Qore Programming Language  1.12.0
QC_HTTPClientPollOperation.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QC_HTTPClientPollOperation.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_CLASS_HTTPCLIENTPOLLOPERATION_H
33 
34 #define _QORE_CLASS_HTTPCLIENTPOLLOPERATION_H
35 
36 #include "qore/intern/QC_AbstractPollOperation.h"
37 #include "qore/QoreHttpClientObject.h"
38 
39 // goals: connect
40 constexpr int HCPG_CONNECT = 1;
41 
42 // states:
43 constexpr int HCPS_NONE = 0;
44 
45 constexpr int SPS_CONNECTING = 1;
46 constexpr int SPS_CONNECTING_SSL = 2;
47 constexpr int SPS_CONNECTED = 3;
48 
49 class HTTPClientPollOperation : public AbstractPollOperation {
50 public:
51  DLLLOCAL HTTPClientPollOperation(ExceptionSink* xsink, const QoreStringNode* goal, const QoreObject* client_obj,
52  QoreHttpClientObject* client) : AbstractPollOperation(goal, client_obj), client(client) {
53  if (*goal == "connect") {
54  sgoal = HCPG_CONNECT;
55 
56  state = SPS_CONNECTING;
57  } else {
58  xsink->raiseException("INVALID-GOAL", "invalid goal \"%s\"; known goal: \"connect\"", goal->c_str());
59  }
60  }
61 
62  DLLLOCAL void deref(ExceptionSink* xsink) {
63  if (ROdereference()) {
64  client->deref(xsink);
65  delete this;
66  }
67  }
68 
69  DLLLOCAL virtual bool goalReached() const {
70  return state == SPS_CONNECTED;
71  }
72 
73  DLLLOCAL virtual QoreHashNode* continuePoll(ExceptionSink* xsink) {
74  QoreHashNode* rv = nullptr;
75 
76  switch (state) {
77  case SPS_CONNECTING: {
78  int rc = checkContinuePoll(xsink);
79  if (rc != 0) {
80  rv = *xsink ? nullptr : getHTTPClientPollInfoHash(xsink, rc);
81  break;
82  }
83 
84  // if we are just connecting, we are done
85  if (sgoal == SPG_CONNECT) {
86  state = SPS_CONNECTED;
87  break;
88  }
89 
90  assert(sgoal == SPG_CONNECT_SSL);
91  state = SPS_CONNECTING_SSL;
92 
93  poll_state.reset(client->startSslConnect(xsink, client->priv->cert ? client->priv->cert->getData() : nullptr,
94  client->priv->pk ? client->priv->pk->getData() : nullptr));
95  if (*xsink) {
96  poll_state.reset();
97  state = SPS_NONE;
98  break;
99  }
100  }
101  // fall down to next case
102 
103  case SPS_CONNECTING_SSL: {
104  int rc = checkContinuePoll(xsink);
105  if (rc != 0) {
106  rv = *xsink ? nullptr : getHTTPClientPollInfoHash(xsink, rc);
107  break;
108  }
109 
110  state = SPS_CONNECTED;
111  break;
112  }
113 
114  default:
115  assert(false);
116  }
117 
118  if (!rv) {
119  if (*xsink) {
120  state = SPS_NONE;
121  } else {
122  state = SPS_CONNECTED;
123  }
124  }
125  return rv;
126  }
127 
128 private:
129  unique_ptr<AbstractPollState> poll_state;
130  QoreHttpClientObject* client;
131 
132  int sgoal = 0;
133  int state = SPS_NONE;
134 
135  DLLLOCAL virtual const char* getStateImpl() const {
136  const char* str;
137  switch (state) {
138  case SPS_NONE:
139  return "none";
140  case SPS_CONNECTING:
141  return "connecting";
142  case SPS_CONNECTING_SSL:
143  return "connecting-ssl";
144  case SPS_CONNECTED:
145  return "connected";
146  default:
147  assert(false);
148  }
149  return "";
150  }
151 
152  DLLLOCAL int checkContinuePoll(ExceptionSink* xsink) {
153  assert(poll_state.get());
154 
155  // see if we are able to continue
156  int rc = poll_state->continuePoll(xsink);
157  //printd(5, "HTTPClientPollOperation::continuePoll() state: %s rc: %d (exp: %d)\n", getStateImpl(), rc,
158  // (int)*xsink);
159  if (*xsink) {
160  assert(rc < 0);
161  state = SPS_NONE;
162  return -1;
163  }
164  if (!rc) {
165  // release the AbstractPollState value
166  poll_state.reset();
167  }
168  return rc;
169  }
170 };
171 
172 DLLLOCAL QoreClass* initHTTPClientPollOperationClass(QoreNamespace& qorens);
173 
174 #endif // _QORE_CLASS_HTTPCLIENTPOLLOPERATION_H
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
defines a Qore-language class
Definition: QoreClass.h:249
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
provides a way to communicate with HTTP servers using Qore data structures
Definition: QoreHttpClientObject.h:51
virtual DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and deletes the object when it reaches 0
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50