intentmedia-capybara-webkit 0.7.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Appraisals +7 -0
- data/CONTRIBUTING.md +38 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +65 -0
- data/LICENSE +19 -0
- data/README.md +67 -0
- data/Rakefile +78 -0
- data/bin/Info.plist +22 -0
- data/capybara-webkit.gemspec +24 -0
- data/extconf.rb +2 -0
- data/gemfiles/1.0.gemfile +7 -0
- data/gemfiles/1.0.gemfile.lock +65 -0
- data/gemfiles/1.1.gemfile +7 -0
- data/gemfiles/1.1.gemfile.lock +65 -0
- data/lib/capybara-webkit.rb +1 -0
- data/lib/capybara/driver/webkit.rb +113 -0
- data/lib/capybara/driver/webkit/browser.rb +216 -0
- data/lib/capybara/driver/webkit/node.rb +118 -0
- data/lib/capybara/driver/webkit/socket_debugger.rb +43 -0
- data/lib/capybara/webkit.rb +11 -0
- data/lib/capybara_webkit_builder.rb +40 -0
- data/spec/browser_spec.rb +178 -0
- data/spec/driver_rendering_spec.rb +80 -0
- data/spec/driver_spec.rb +1048 -0
- data/spec/integration/driver_spec.rb +20 -0
- data/spec/integration/session_spec.rb +137 -0
- data/spec/self_signed_ssl_cert.rb +42 -0
- data/spec/spec_helper.rb +25 -0
- data/src/Body.h +12 -0
- data/src/ClearCookies.cpp +18 -0
- data/src/ClearCookies.h +11 -0
- data/src/Command.cpp +15 -0
- data/src/Command.h +29 -0
- data/src/CommandFactory.cpp +29 -0
- data/src/CommandFactory.h +16 -0
- data/src/CommandParser.cpp +68 -0
- data/src/CommandParser.h +29 -0
- data/src/Connection.cpp +82 -0
- data/src/Connection.h +36 -0
- data/src/Evaluate.cpp +84 -0
- data/src/Evaluate.h +22 -0
- data/src/Execute.cpp +16 -0
- data/src/Execute.h +12 -0
- data/src/Find.cpp +19 -0
- data/src/Find.h +13 -0
- data/src/FrameFocus.cpp +66 -0
- data/src/FrameFocus.h +28 -0
- data/src/GetCookies.cpp +22 -0
- data/src/GetCookies.h +14 -0
- data/src/Header.cpp +18 -0
- data/src/Header.h +11 -0
- data/src/Headers.cpp +11 -0
- data/src/Headers.h +12 -0
- data/src/JavascriptInvocation.cpp +14 -0
- data/src/JavascriptInvocation.h +19 -0
- data/src/NetworkAccessManager.cpp +25 -0
- data/src/NetworkAccessManager.h +18 -0
- data/src/NetworkCookieJar.cpp +101 -0
- data/src/NetworkCookieJar.h +15 -0
- data/src/Node.cpp +14 -0
- data/src/Node.h +13 -0
- data/src/Render.cpp +19 -0
- data/src/Render.h +12 -0
- data/src/Reset.cpp +20 -0
- data/src/Reset.h +12 -0
- data/src/Response.cpp +19 -0
- data/src/Response.h +13 -0
- data/src/Server.cpp +25 -0
- data/src/Server.h +21 -0
- data/src/SetCookie.cpp +18 -0
- data/src/SetCookie.h +11 -0
- data/src/SetProxy.cpp +24 -0
- data/src/SetProxy.h +11 -0
- data/src/Source.cpp +20 -0
- data/src/Source.h +19 -0
- data/src/Status.cpp +13 -0
- data/src/Status.h +12 -0
- data/src/UnsupportedContentHandler.cpp +32 -0
- data/src/UnsupportedContentHandler.h +18 -0
- data/src/Url.cpp +15 -0
- data/src/Url.h +12 -0
- data/src/Visit.cpp +21 -0
- data/src/Visit.h +15 -0
- data/src/WebPage.cpp +226 -0
- data/src/WebPage.h +54 -0
- data/src/body.cpp +11 -0
- data/src/capybara.js +205 -0
- data/src/find_command.h +24 -0
- data/src/main.cpp +34 -0
- data/src/webkit_server.pro +71 -0
- data/src/webkit_server.qrc +5 -0
- data/templates/Command.cpp +10 -0
- data/templates/Command.h +12 -0
- data/webkit_server.pro +4 -0
- metadata +246 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'capybara/driver/webkit'
|
3
|
+
|
4
|
+
describe Capybara::Driver::Webkit do
|
5
|
+
before do
|
6
|
+
@driver = Capybara::Driver::Webkit.new(TestApp, :browser => $webkit_browser)
|
7
|
+
end
|
8
|
+
|
9
|
+
it_should_behave_like "driver"
|
10
|
+
it_should_behave_like "driver with javascript support"
|
11
|
+
it_should_behave_like "driver with cookies support"
|
12
|
+
it_should_behave_like "driver with header support"
|
13
|
+
it_should_behave_like "driver with status code support"
|
14
|
+
it_should_behave_like "driver with frame support"
|
15
|
+
|
16
|
+
it "returns the rack server port" do
|
17
|
+
@driver.server_port.should eq(@driver.instance_variable_get(:@rack_server).port)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# -*- encoding: UTF-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'capybara/webkit'
|
5
|
+
|
6
|
+
describe Capybara::Session do
|
7
|
+
subject { Capybara::Session.new(:reusable_webkit, @app) }
|
8
|
+
after { subject.reset! }
|
9
|
+
|
10
|
+
context "slow javascript app" do
|
11
|
+
before(:all) do
|
12
|
+
@app = lambda do |env|
|
13
|
+
body = <<-HTML
|
14
|
+
<html><body>
|
15
|
+
<form action="/next" id="submit_me"><input type="submit" value="Submit" /></form>
|
16
|
+
<p id="change_me">Hello</p>
|
17
|
+
|
18
|
+
<script type="text/javascript">
|
19
|
+
var form = document.getElementById('submit_me');
|
20
|
+
form.addEventListener("submit", function (event) {
|
21
|
+
event.preventDefault();
|
22
|
+
setTimeout(function () {
|
23
|
+
document.getElementById("change_me").innerHTML = 'Good' + 'bye';
|
24
|
+
}, 500);
|
25
|
+
});
|
26
|
+
</script>
|
27
|
+
</body></html>
|
28
|
+
HTML
|
29
|
+
[200,
|
30
|
+
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
|
31
|
+
[body]]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
before do
|
36
|
+
@default_wait_time = Capybara.default_wait_time
|
37
|
+
Capybara.default_wait_time = 1
|
38
|
+
end
|
39
|
+
|
40
|
+
after { Capybara.default_wait_time = @default_wait_time }
|
41
|
+
|
42
|
+
it "waits for a request to load" do
|
43
|
+
subject.visit("/")
|
44
|
+
subject.find_button("Submit").click
|
45
|
+
subject.should have_content("Goodbye");
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "simple app" do
|
50
|
+
before(:all) do
|
51
|
+
@app = lambda do |env|
|
52
|
+
body = <<-HTML
|
53
|
+
<html><body>
|
54
|
+
<strong>Hello</strong>
|
55
|
+
<span>UTF8文字列</span>
|
56
|
+
<input type="button" value="ボタン" />
|
57
|
+
</body></html>
|
58
|
+
HTML
|
59
|
+
[200,
|
60
|
+
{ 'Content-Type' => 'text/html; charset=UTF-8', 'Content-Length' => body.length.to_s },
|
61
|
+
[body]]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
before do
|
66
|
+
subject.visit("/")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "inspects nodes" do
|
70
|
+
subject.all(:xpath, "//strong").first.inspect.should include("strong")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "can read utf8 string" do
|
74
|
+
utf8str = subject.all(:xpath, "//span").first.text
|
75
|
+
utf8str.should eq('UTF8文字列')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "can click utf8 string" do
|
79
|
+
subject.click_button('ボタン')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "response headers with status code" do
|
84
|
+
before(:all) do
|
85
|
+
@app = lambda do |env|
|
86
|
+
params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
|
87
|
+
if params["img"] == "true"
|
88
|
+
body = 'not found'
|
89
|
+
return [404, { 'Content-Type' => 'image/gif', 'Content-Length' => body.length.to_s }, [body]]
|
90
|
+
end
|
91
|
+
body = <<-HTML
|
92
|
+
<html>
|
93
|
+
<body>
|
94
|
+
<img src="?img=true">
|
95
|
+
</body>
|
96
|
+
</html>
|
97
|
+
HTML
|
98
|
+
[200,
|
99
|
+
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s, 'X-Capybara' => 'WebKit'},
|
100
|
+
[body]]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should get status code" do
|
105
|
+
subject.visit '/'
|
106
|
+
subject.status_code.should == 200
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should reset status code" do
|
110
|
+
subject.visit '/'
|
111
|
+
subject.status_code.should == 200
|
112
|
+
subject.reset!
|
113
|
+
subject.status_code.should == 0
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should get response headers" do
|
117
|
+
subject.visit '/'
|
118
|
+
subject.response_headers['X-Capybara'].should == 'WebKit'
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should reset response headers" do
|
122
|
+
subject.visit '/'
|
123
|
+
subject.response_headers['X-Capybara'].should == 'WebKit'
|
124
|
+
subject.reset!
|
125
|
+
subject.response_headers['X-Capybara'].should == nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe Capybara::Session, "with TestApp" do
|
131
|
+
before do
|
132
|
+
@session = Capybara::Session.new(:reusable_webkit, TestApp)
|
133
|
+
end
|
134
|
+
|
135
|
+
it_should_behave_like "session"
|
136
|
+
it_should_behave_like "session with javascript support"
|
137
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
pem = <<-PEM_ENCODED
|
4
|
+
-----BEGIN PRIVATE KEY-----
|
5
|
+
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKW+grT6YW3gv79y
|
6
|
+
P9JkQDtm3cDSUhAhd/TEyRBt/pSKz3pNSygsleBJl2d7g8k0fteec95a7YnYRKGH
|
7
|
+
XhIpUOvl/3uaV2NVipqxwB+Z+0M+7HegxL3e4unaRFy9kf9/UXJzmuA9BTMLrm/w
|
8
|
+
IoW17f+dz7BIFZhCvRurkrmvzraNAgMBAAECgYBv4uB3bYJx20N16Jk+3OAjeXh/
|
9
|
+
Hzu4me9Rc7pLdgVinyYaaK0wrJBsfSFRASdgnyh1RAjx9K3f3PfPlwMg/XUbA6Yd
|
10
|
+
YOYlMnBUwCJX09TH8RFFCzJzbBylpk/sTF1geICln2O2BloT2cM24PlEPvyz1xLa
|
11
|
+
XvxCOnJJfgNU1K6kvQJBANcEVyOMJ9RBfI8gj1o7S70J9yJRI4jvqxuvcOxJBCi6
|
12
|
+
CDatkh/getHswsE3sLj25VhrNsi6UQcN8Bjm8Yjt8BsCQQDFVe0uCwobseprMOuW
|
13
|
+
dPU4+saN1cFnIT5Gp0iwYRPinjUlkh6H/AuUtUulKFXVmxk1KElpp1E3bxpCDgCp
|
14
|
+
oe53AkArO1Mt8Ys8kSIzQO+xy8RRsQRAoSHM8atsuJyy1YeBjM4D+GguApuPQ9Rw
|
15
|
+
tvrQZcv9OCleuJ98FKBW0XB1AKpLAkEAmOR3bJofDdAuWTjA/4TEzo32MsRwIZBv
|
16
|
+
KNzJg+bjOkzrzp1EzIVrD5/b6S20O1j9EeOR5as+UN3jEVS6DLQrBwJAe5OTrDiQ
|
17
|
+
vKtUaAwquC4f4Ia05KwJw+vFrPVaOqgc4QLdxRwx4PfV/Uw3OOqMolpPAvpUi9JI
|
18
|
+
LAwIaTtCvo18OQ==
|
19
|
+
-----END PRIVATE KEY-----
|
20
|
+
-----BEGIN CERTIFICATE-----
|
21
|
+
MIICgDCCAemgAwIBAgIJANWcyeZB2ql1MA0GCSqGSIb3DQEBBQUAMFkxCzAJBgNV
|
22
|
+
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
|
23
|
+
aWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xMTA5MjgyMDIx
|
24
|
+
MTFaFw0xMjA5MjcyMDIxMTFaMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21l
|
25
|
+
LVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNV
|
26
|
+
BAMMCWxvY2FsaG9zdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEApb6CtPph
|
27
|
+
beC/v3I/0mRAO2bdwNJSECF39MTJEG3+lIrPek1LKCyV4EmXZ3uDyTR+155z3lrt
|
28
|
+
idhEoYdeEilQ6+X/e5pXY1WKmrHAH5n7Qz7sd6DEvd7i6dpEXL2R/39RcnOa4D0F
|
29
|
+
Mwuub/AihbXt/53PsEgVmEK9G6uSua/Oto0CAwEAAaNQME4wHQYDVR0OBBYEFBAm
|
30
|
+
x19zpY8M8FEcMXB4WQeUhqIiMB8GA1UdIwQYMBaAFBAmx19zpY8M8FEcMXB4WQeU
|
31
|
+
hqIiMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAcznCusWS5Ws77IUl
|
32
|
+
b87vdfCPVphICyfGHGWhHp3BZ3WLOphauAMdOYIiJGtPExyWr4DYpzbvx0+Ljg7G
|
33
|
+
2FNaI0QRXqbR5ccpvwm6KELmU0XDhykNaXiXSdnvIanr3z/hZ5e03mXAywo+nGQj
|
34
|
+
UYTVCb6g/uHVNzXq1NgHGuqogKY=
|
35
|
+
-----END CERTIFICATE-----
|
36
|
+
PEM_ENCODED
|
37
|
+
|
38
|
+
key = OpenSSL::PKey::RSA.new(pem)
|
39
|
+
cert = OpenSSL::X509::Certificate.new(pem)
|
40
|
+
$openssl_self_signed_ctx = OpenSSL::SSL::SSLContext.new
|
41
|
+
$openssl_self_signed_ctx.key = key
|
42
|
+
$openssl_self_signed_ctx.cert = cert
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
|
4
|
+
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
|
5
|
+
|
6
|
+
$LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
|
7
|
+
|
8
|
+
Dir[File.join(PROJECT_ROOT, 'spec', 'support', '**', '*.rb')].each { |file| require(file) }
|
9
|
+
|
10
|
+
spec_dir = nil
|
11
|
+
$:.detect do |dir|
|
12
|
+
if File.exists? File.join(dir, "capybara.rb")
|
13
|
+
spec_dir = File.expand_path(File.join(dir,"..","spec"))
|
14
|
+
$:.unshift( spec_dir )
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require File.join(spec_dir,"spec_helper")
|
19
|
+
|
20
|
+
require 'capybara/driver/webkit/browser'
|
21
|
+
$webkit_browser = Capybara::Driver::Webkit::Browser.new(:socket_class => TCPSocket, :stdout => nil)
|
22
|
+
|
23
|
+
Capybara.register_driver :reusable_webkit do |app|
|
24
|
+
Capybara::Driver::Webkit.new(app, :browser => $webkit_browser)
|
25
|
+
end
|
data/src/Body.h
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#include "ClearCookies.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
#include "NetworkCookieJar.h"
|
4
|
+
#include <QNetworkCookie>
|
5
|
+
|
6
|
+
ClearCookies::ClearCookies(WebPage *page, QObject *parent)
|
7
|
+
: Command(page, parent)
|
8
|
+
{ }
|
9
|
+
|
10
|
+
void ClearCookies::start(QStringList &arguments)
|
11
|
+
{
|
12
|
+
Q_UNUSED(arguments);
|
13
|
+
NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
|
14
|
+
->networkAccessManager()
|
15
|
+
->cookieJar());
|
16
|
+
jar->clearCookies();
|
17
|
+
emit finished(new Response(true));
|
18
|
+
}
|
data/src/ClearCookies.h
ADDED
data/src/Command.cpp
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#include "Command.h"
|
2
|
+
#include "WebPage.h"
|
3
|
+
|
4
|
+
Command::Command(WebPage *page, QObject *parent) : QObject(parent) {
|
5
|
+
m_page = page;
|
6
|
+
}
|
7
|
+
|
8
|
+
void Command::start(QStringList &arguments) {
|
9
|
+
Q_UNUSED(arguments);
|
10
|
+
}
|
11
|
+
|
12
|
+
WebPage *Command::page() {
|
13
|
+
return m_page;
|
14
|
+
}
|
15
|
+
|
data/src/Command.h
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#ifndef COMMAND_H
|
2
|
+
#define COMMAND_H
|
3
|
+
|
4
|
+
#include <QObject>
|
5
|
+
#include <QStringList>
|
6
|
+
#include "Response.h"
|
7
|
+
|
8
|
+
class WebPage;
|
9
|
+
|
10
|
+
class Command : public QObject {
|
11
|
+
Q_OBJECT
|
12
|
+
|
13
|
+
public:
|
14
|
+
Command(WebPage *page, QObject *parent = 0);
|
15
|
+
virtual void start(QStringList &arguments);
|
16
|
+
|
17
|
+
signals:
|
18
|
+
void finished(Response *response);
|
19
|
+
|
20
|
+
protected:
|
21
|
+
WebPage *page();
|
22
|
+
|
23
|
+
private:
|
24
|
+
WebPage *m_page;
|
25
|
+
|
26
|
+
};
|
27
|
+
|
28
|
+
#endif
|
29
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#include "CommandFactory.h"
|
2
|
+
#include "Visit.h"
|
3
|
+
#include "Find.h"
|
4
|
+
#include "Command.h"
|
5
|
+
#include "Reset.h"
|
6
|
+
#include "Node.h"
|
7
|
+
#include "Url.h"
|
8
|
+
#include "Source.h"
|
9
|
+
#include "Evaluate.h"
|
10
|
+
#include "Execute.h"
|
11
|
+
#include "FrameFocus.h"
|
12
|
+
#include "Header.h"
|
13
|
+
#include "Render.h"
|
14
|
+
#include "Body.h"
|
15
|
+
#include "Status.h"
|
16
|
+
#include "Headers.h"
|
17
|
+
#include "SetCookie.h"
|
18
|
+
#include "ClearCookies.h"
|
19
|
+
#include "GetCookies.h"
|
20
|
+
#include "SetProxy.h"
|
21
|
+
|
22
|
+
CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
|
23
|
+
m_page = page;
|
24
|
+
}
|
25
|
+
|
26
|
+
Command *CommandFactory::createCommand(const char *name) {
|
27
|
+
#include "find_command.h"
|
28
|
+
return NULL;
|
29
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#include <QObject>
|
2
|
+
|
3
|
+
class Command;
|
4
|
+
class WebPage;
|
5
|
+
|
6
|
+
class CommandFactory : public QObject {
|
7
|
+
Q_OBJECT
|
8
|
+
|
9
|
+
public:
|
10
|
+
CommandFactory(WebPage *page, QObject *parent = 0);
|
11
|
+
Command *createCommand(const char *name);
|
12
|
+
|
13
|
+
private:
|
14
|
+
WebPage *m_page;
|
15
|
+
};
|
16
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
#include "CommandParser.h"
|
2
|
+
|
3
|
+
#include <QIODevice>
|
4
|
+
|
5
|
+
CommandParser::CommandParser(QIODevice *device, QObject *parent) :
|
6
|
+
QObject(parent) {
|
7
|
+
m_device = device;
|
8
|
+
m_expectingDataSize = -1;
|
9
|
+
connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext()));
|
10
|
+
}
|
11
|
+
|
12
|
+
void CommandParser::checkNext() {
|
13
|
+
if (m_expectingDataSize == -1) {
|
14
|
+
if (m_device->canReadLine()) {
|
15
|
+
readLine();
|
16
|
+
checkNext();
|
17
|
+
}
|
18
|
+
} else {
|
19
|
+
if (m_device->bytesAvailable() >= m_expectingDataSize) {
|
20
|
+
readDataBlock();
|
21
|
+
checkNext();
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
void CommandParser::readLine() {
|
27
|
+
char buffer[128];
|
28
|
+
qint64 lineLength = m_device->readLine(buffer, 128);
|
29
|
+
if (lineLength != -1) {
|
30
|
+
buffer[lineLength - 1] = 0;
|
31
|
+
processNext(buffer);
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
void CommandParser::readDataBlock() {
|
36
|
+
char *buffer = new char[m_expectingDataSize + 1];
|
37
|
+
m_device->read(buffer, m_expectingDataSize);
|
38
|
+
buffer[m_expectingDataSize] = 0;
|
39
|
+
processNext(buffer);
|
40
|
+
m_expectingDataSize = -1;
|
41
|
+
delete[] buffer;
|
42
|
+
}
|
43
|
+
|
44
|
+
void CommandParser::processNext(const char *data) {
|
45
|
+
if (m_commandName.isNull()) {
|
46
|
+
m_commandName = data;
|
47
|
+
m_argumentsExpected = -1;
|
48
|
+
} else {
|
49
|
+
processArgument(data);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
void CommandParser::processArgument(const char *data) {
|
54
|
+
if (m_argumentsExpected == -1) {
|
55
|
+
m_argumentsExpected = QString(data).toInt();
|
56
|
+
} else if (m_expectingDataSize == -1) {
|
57
|
+
m_expectingDataSize = QString(data).toInt();
|
58
|
+
} else {
|
59
|
+
m_arguments.append(QString::fromUtf8(data));
|
60
|
+
}
|
61
|
+
|
62
|
+
if (m_arguments.length() == m_argumentsExpected) {
|
63
|
+
emit commandReady(m_commandName, m_arguments);
|
64
|
+
m_commandName = QString();
|
65
|
+
m_arguments.clear();
|
66
|
+
m_argumentsExpected = -1;
|
67
|
+
}
|
68
|
+
}
|
data/src/CommandParser.h
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#include <QObject>
|
2
|
+
#include <QStringList>
|
3
|
+
|
4
|
+
class QIODevice;
|
5
|
+
|
6
|
+
class CommandParser : public QObject {
|
7
|
+
Q_OBJECT
|
8
|
+
|
9
|
+
public:
|
10
|
+
CommandParser(QIODevice *device, QObject *parent = 0);
|
11
|
+
|
12
|
+
public slots:
|
13
|
+
void checkNext();
|
14
|
+
|
15
|
+
signals:
|
16
|
+
void commandReady(QString commandName, QStringList arguments);
|
17
|
+
|
18
|
+
private:
|
19
|
+
void readLine();
|
20
|
+
void readDataBlock();
|
21
|
+
void processNext(const char *line);
|
22
|
+
void processArgument(const char *data);
|
23
|
+
QIODevice *m_device;
|
24
|
+
QString m_commandName;
|
25
|
+
QStringList m_arguments;
|
26
|
+
int m_argumentsExpected;
|
27
|
+
int m_expectingDataSize;
|
28
|
+
};
|
29
|
+
|