File: xmlhttprequest.h

package info (click to toggle)
khtml 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,940 kB
  • sloc: cpp: 206,281; java: 4,060; ansic: 2,829; perl: 2,313; yacc: 1,497; python: 339; sh: 141; xml: 37; makefile: 7
file content (200 lines) | stat: -rw-r--r-- 5,589 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2003 Apple Computer, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _XMLHTTPREQUEST_H_
#define _XMLHTTPREQUEST_H_

#include "ecma/kjs_binding.h"
#include "ecma/kjs_dom.h"
#include <kencodingdetector.h>
#include "kio/jobclasses.h"
#include <QPointer>
#include <QHash>

#include "xml/dom_nodeimpl.h"

namespace KJS
{

class JSEventListener;
class XMLHttpRequestQObject;

class CaseInsensitiveString
{
public:
    CaseInsensitiveString(const char *s) : str(QLatin1String(s)) { }
    CaseInsensitiveString(const QString &s) : str(s) { }

    QString original() const
    {
        return str;
    }
    QString toLower() const
    {
        return str.toLower();
    }

private:
    QString str;
};

inline bool operator==(const CaseInsensitiveString &a,
                       const CaseInsensitiveString &b)
{
    return a.original().compare(b.original(), Qt::CaseInsensitive) == 0;
}

inline uint qHash(const CaseInsensitiveString &key)
{
    return qHash(key.toLower());
}

typedef QHash<CaseInsensitiveString, QString> HTTPHeaderMap;

// these exact numeric values are important because JS expects them
enum XMLHttpRequestState {
    XHRS_Uninitialized = 0,
    XHRS_Open = 1,
    XHRS_Sent = 2,
    XHRS_Receiving = 3,
    XHRS_Loaded = 4
};

class XMLHttpRequestConstructorImp : public JSObject
{
public:
    XMLHttpRequestConstructorImp(ExecState *exec, DOM::DocumentImpl *d);
    bool implementsConstruct() const override;
    using KJS::JSObject::construct;
    JSObject *construct(ExecState *exec, const List &args) override;
private:
    SharedPtr<DOM::DocumentImpl> doc;
};

class XMLHttpRequest : public DOMObject, public DOM::EventTargetImpl
{
public:
    XMLHttpRequest(ExecState *, DOM::DocumentImpl *d);
    ~XMLHttpRequest();

    Type eventTargetType() const override
    {
        return XML_HTTP_REQUEST;
    }

    using KJS::JSObject::getOwnPropertySlot;
    bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
    JSValue *getValueProperty(ExecState *exec, int token) const;
    using KJS::JSObject::put;
    void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override;
    void putValueProperty(ExecState *exec, int token, JSValue *value, int /*attr*/);
    bool toBoolean(ExecState *) const override
    {
        return true;
    }
    const ClassInfo *classInfo() const override
    {
        return &info;
    }
    static const ClassInfo info;
    enum { Onload, Onreadystatechange, ReadyState, ResponseText, ResponseXML, Status, StatusText, Abort,
           GetAllResponseHeaders, GetResponseHeader, Open, Send, SetRequestHeader,
           OverrideMIMEType
         };

private:
    friend class XMLHttpRequestProtoFunc;
    friend class XMLHttpRequestQObject;

    JSValue *getStatusText() const;
    JSValue *getStatus() const;
    bool urlMatchesDocumentDomain(const QUrl &) const;

    XMLHttpRequestQObject *qObject;

#ifdef APPLE_CHANGES
    void slotData(KIO::Job *job, const char *data, int size);
#else
    void slotData(KIO::Job *job, const QByteArray &data);
#endif
    void slotFinished(KJob *);
    void slotRedirection(KIO::Job *, const QUrl &);

    void processSyncLoadResults(const QByteArray &data, const QUrl &finalURL, const QString &headers);

    void open(const QString &_method, const QUrl &_url, bool _async, int &ec);
    void send(const QString &_body, int &ec);
    void abort();
    void setRequestHeader(const QString &name, const QString &value, int &ec);
    void overrideMIMEType(const QString &override);
    JSValue *getAllResponseHeaders(int &ec) const;
    JSValue *getResponseHeader(const QString &name, int &ec) const;

    void changeState(XMLHttpRequestState newState);

    QPointer<DOM::DocumentImpl> doc;

    QUrl url;
    QString m_method;
    bool async;
    HTTPHeaderMap m_requestHeaders;
    QString m_mimeTypeOverride;
    QString contentType;

    KIO::StoredTransferJob *job;

    XMLHttpRequestState m_state;
    JSEventListener *onReadyStateChangeListener;
    JSEventListener *onLoadListener;

    KEncodingDetector *decoder;
    bool              binaryMode; // just byte-expand data,
    // don't pass it through the decoder.
    void clearDecoder();

    QString encoding;
    QString responseHeaders;

    QString response;
    mutable bool createdDocument;
    mutable bool typeIsXML;
    mutable SharedPtr<DOM::DocumentImpl> responseXML;

    bool aborted;
};

class XMLHttpRequestQObject : public QObject
{
    Q_OBJECT

public:
    XMLHttpRequestQObject(XMLHttpRequest *_jsObject);

public Q_SLOTS:
    void slotData(KIO::Job *job, const QByteArray &data);
    void slotFinished(KJob *job);
    void slotRedirection(KIO::Job *job, const QUrl &url);

private:
    XMLHttpRequest *jsObject;
};

} // namespace

#endif