KHtml

xmlhttprequest.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2003 Apple Computer, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef _XMLHTTPREQUEST_H_
21 #define _XMLHTTPREQUEST_H_
22 
23 #include "ecma/kjs_binding.h"
24 #include "ecma/kjs_dom.h"
25 #include <kencodingdetector.h>
26 #include "kio/jobclasses.h"
27 #include <QPointer>
28 #include <QHash>
29 
30 #include "xml/dom_nodeimpl.h"
31 
32 namespace KJS
33 {
34 
35 class JSEventListener;
36 class XMLHttpRequestQObject;
37 
38 class CaseInsensitiveString
39 {
40 public:
41  CaseInsensitiveString(const char *s) : str(QLatin1String(s)) { }
42  CaseInsensitiveString(const QString &s) : str(s) { }
43 
44  QString original() const
45  {
46  return str;
47  }
48  QString toLower() const
49  {
50  return str.toLower();
51  }
52 
53 private:
54  QString str;
55 };
56 
57 inline bool operator==(const CaseInsensitiveString &a,
58  const CaseInsensitiveString &b)
59 {
60  return a.original().compare(b.original(), Qt::CaseInsensitive) == 0;
61 }
62 
63 inline uint qHash(const CaseInsensitiveString &key)
64 {
65  return qHash(key.toLower());
66 }
67 
68 typedef QHash<CaseInsensitiveString, QString> HTTPHeaderMap;
69 
70 // these exact numeric values are important because JS expects them
71 enum XMLHttpRequestState {
72  XHRS_Uninitialized = 0,
73  XHRS_Open = 1,
74  XHRS_Sent = 2,
75  XHRS_Receiving = 3,
76  XHRS_Loaded = 4
77 };
78 
79 class XMLHttpRequestConstructorImp : public JSObject
80 {
81 public:
82  XMLHttpRequestConstructorImp(ExecState *exec, DOM::DocumentImpl *d);
83  bool implementsConstruct() const override;
84  using KJS::JSObject::construct;
85  JSObject *construct(ExecState *exec, const List &args) override;
86 private:
87  SharedPtr<DOM::DocumentImpl> doc;
88 };
89 
90 class XMLHttpRequest : public DOMObject, public DOM::EventTargetImpl
91 {
92 public:
93  XMLHttpRequest(ExecState *, DOM::DocumentImpl *d);
94  ~XMLHttpRequest();
95 
96  Type eventTargetType() const override
97  {
98  return XML_HTTP_REQUEST;
99  }
100 
101  using KJS::JSObject::getOwnPropertySlot;
102  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
103  JSValue *getValueProperty(ExecState *exec, int token) const;
104  using KJS::JSObject::put;
105  void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override;
106  void putValueProperty(ExecState *exec, int token, JSValue *value, int /*attr*/);
107  bool toBoolean(ExecState *) const override
108  {
109  return true;
110  }
111  const ClassInfo *classInfo() const override
112  {
113  return &info;
114  }
115  static const ClassInfo info;
116  enum { Onload, Onreadystatechange, ReadyState, ResponseText, ResponseXML, Status, StatusText, Abort,
117  GetAllResponseHeaders, GetResponseHeader, Open, Send, SetRequestHeader,
118  OverrideMIMEType
119  };
120 
121 private:
122  friend class XMLHttpRequestProtoFunc;
123  friend class XMLHttpRequestQObject;
124 
125  JSValue *getStatusText() const;
126  JSValue *getStatus() const;
127  bool urlMatchesDocumentDomain(const QUrl &) const;
128 
129  XMLHttpRequestQObject *qObject;
130 
131 #ifdef APPLE_CHANGES
132  void slotData(KIO::Job *job, const char *data, int size);
133 #else
134  void slotData(KIO::Job *job, const QByteArray &data);
135 #endif
136  void slotFinished(KJob *);
137  void slotRedirection(KIO::Job *, const QUrl &);
138 
139  void processSyncLoadResults(const QByteArray &data, const QUrl &finalURL, const QString &headers);
140 
141  void open(const QString &_method, const QUrl &_url, bool _async, int &ec);
142  void send(const QString &_body, int &ec);
143  void abort();
144  void setRequestHeader(const QString &name, const QString &value, int &ec);
145  void overrideMIMEType(const QString &override);
146  JSValue *getAllResponseHeaders(int &ec) const;
147  JSValue *getResponseHeader(const QString &name, int &ec) const;
148 
149  void changeState(XMLHttpRequestState newState);
150 
152 
153  QUrl url;
154  QString m_method;
155  bool async;
156  HTTPHeaderMap m_requestHeaders;
157  QString m_mimeTypeOverride;
158  QString contentType;
159 
161 
162  XMLHttpRequestState m_state;
163  JSEventListener *onReadyStateChangeListener;
164  JSEventListener *onLoadListener;
165 
166  KEncodingDetector *decoder;
167  bool binaryMode; // just byte-expand data,
168  // don't pass it through the decoder.
169  void clearDecoder();
170 
171  QString encoding;
172  QString responseHeaders;
173 
174  QString response;
175  mutable bool createdDocument;
176  mutable bool typeIsXML;
177  mutable SharedPtr<DOM::DocumentImpl> responseXML;
178 
179  bool aborted;
180 };
181 
182 class XMLHttpRequestQObject : public QObject
183 {
184  Q_OBJECT
185 
186 public:
187  XMLHttpRequestQObject(XMLHttpRequest *_jsObject);
188 
189 public Q_SLOTS:
190  void slotData(KIO::Job *job, const QByteArray &data);
191  void slotFinished(KJob *job);
192  void slotRedirection(KIO::Job *job, const QUrl &url);
193 
194 private:
195  XMLHttpRequest *jsObject;
196 };
197 
198 } // namespace
199 
200 #endif
Provides encoding detection capabilities.
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
CaseInsensitive
KIOCORE_EXPORT TransferJob * put(const QUrl &url, int permissions, JobFlags flags=DefaultFlags)
QString toLower() const const
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
KIOCORE_EXPORT FileJob * open(const QUrl &url, QIODevice::OpenMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.