Skip to content

Commit

Permalink
discover http header workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuoqing committed Mar 11, 2017
1 parent eae1d3e commit 3f7acce
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 4 deletions.
10 changes: 10 additions & 0 deletions kmymoney/plugins/ofximport/dialogs/kofxdirectconnectdlg.cpp
Expand Up @@ -45,6 +45,7 @@
#include <mymoneyinstitution.h>
#include <mymoneyfile.h>
#include "mymoneyofxconnector.h"
#include "../ofxpartner.h"

class KOfxDirectConnectDlg::Private
{
Expand Down Expand Up @@ -109,6 +110,15 @@ bool KOfxDirectConnectDlg::init()
d->m_fpTrace.write("response:\n", 10);
}

KTemporaryFile tmpDownload;
if (tmpDownload.open() && OfxPythonHttpsRequest("POST", m_connector.url(), request, QMap<QString, QString>(), tmpDownload.fileName())) {
qDebug("OfxPython Emit statementReady signal with '%s'", qPrintable(tmpDownload.fileName()));
emit statementReady(tmpDownload.fileName());
qDebug("OfxPython Return from signal statementReady() processing");
hide();
return false;
}

qDebug("creating job");
m_job = KIO::http_post(m_connector.url(), request, KIO::HideProgressInfo);

Expand Down
66 changes: 62 additions & 4 deletions kmymoney/plugins/ofximport/ofxpartner.cpp
Expand Up @@ -374,6 +374,61 @@ class OfxHttpsRequest::Private
QFile m_fpTrace;
};

#include <QProcess>
#include <ktemporaryfile.h>
bool OfxPythonHttpsRequest(const QString& method, const KUrl &url, const QByteArray &postData, const QMap<QString, QString>& metaData, const QString& dst)
{
QDir homeDir(QDir::home());
if (!homeDir.exists("ofx_pythondownload.py")) {
return false;
}
KTemporaryFile tmpPost;
if (!tmpPost.open()) {
qWarning("Unable to open tempfile '%s' for post.", qPrintable(tmpPost.fileName()));
return false;
}
tmpPost.write(postData);
tmpPost.close();
qDebug("tempfile '%s' for post data", qPrintable(tmpPost.fileName()));

KTemporaryFile tmpMeta;
if (!tmpMeta.open()) {
qWarning("Unable to open tempfile '%s' for metaData.", qPrintable(tmpMeta.fileName()));
return false;
}

QMap<QString, QString>::const_iterator it = metaData.begin();
while (it != metaData.end())
{
QByteArray dataKey = it.key().toUtf8();
tmpMeta.write(dataKey, strlen(dataKey));
tmpMeta.write("\n", 1);
QByteArray dataValue = it.value().toUtf8();
tmpMeta.write(dataValue, strlen(dataValue));
tmpMeta.write("\n", 1);
++it;
}
tmpMeta.close();
qDebug("tempfile '%s' for metaData", qPrintable(tmpMeta.fileName()));

QString pythonScript = QString("%1/ofx_pythondownload.py").arg(QDir::homePath());

QProcess pyDownload;
pyDownload.start("python", QStringList() << pythonScript << method << url.url() << tmpMeta.fileName() << tmpPost.fileName() << dst);
if (!pyDownload.waitForStarted())
return false;

if (!pyDownload.waitForFinished(-1))
return false;

int exitCode = pyDownload.exitCode();
qDebug("pyDownload exit with '%d'", exitCode);
if (exitCode != 0)
return false;

return true;
}

OfxHttpsRequest::OfxHttpsRequest(const QString& type, const KUrl &url, const QByteArray &postData, const QMap<QString, QString>& metaData, const KUrl& dst, bool showProgressInfo) :
d(new Private),
m_dst(dst),
Expand All @@ -382,6 +437,9 @@ OfxHttpsRequest::OfxHttpsRequest(const QString& type, const KUrl &url, const QBy
Q_UNUSED(type);
Q_UNUSED(metaData);

if (OfxPythonHttpsRequest(type, url, postData, metaData, dst.path()))
return;

m_eventLoop = new QEventLoop(qApp->activeWindow());

QDir homeDir(QDir::home());
Expand All @@ -395,8 +453,10 @@ OfxHttpsRequest::OfxHttpsRequest(const QString& type, const KUrl &url, const QBy
jobFlags = KIO::HideProgressInfo;

m_job = KIO::http_post(url, postData, jobFlags);
m_job->addMetaData("content-type", "Content-type: application/x-ofx");

m_job->addMetaData("content-type", "Content-Type: application/x-ofx");
//m_job->addMetaData("2", QString("Host: ").append(url.host()));
//m_job->addMetaData("3", QString("Content-Length: ").append(QString::number(postData.length())));
//m_job->addMetaData("4", "Connection: Keep-Alive");
if (d->m_fpTrace.isOpen()) {
QTextStream ts(&d->m_fpTrace);
ts << "url: " << url.prettyUrl() << "\n";
Expand Down Expand Up @@ -477,8 +537,6 @@ void OfxHttpsRequest::slotOfxFinished(KJob* /* e */)
m_eventLoop->exit();
}



OfxHttpRequest::OfxHttpRequest(const QString& type, const KUrl &url, const QByteArray &postData, const QMap<QString, QString>& metaData, const KUrl& dst, bool showProgressInfo) :
m_job(0)
{
Expand Down
2 changes: 2 additions & 0 deletions kmymoney/plugins/ofximport/ofxpartner.h
Expand Up @@ -61,6 +61,8 @@ QStringList FipidForBank(const QString& bank);

}

bool OfxPythonHttpsRequest(const QString& method, const KUrl &url, const QByteArray &postData, const QMap<QString, QString>& metaData, const QString& dst);

class OfxHttpRequest : public QObject
{
Q_OBJECT
Expand Down
62 changes: 62 additions & 0 deletions ofx_pythondownload.py
@@ -0,0 +1,62 @@
#!/usr/bin/python
import sys, os
#import requests
import httplib, ssl
from collections import OrderedDict
import logging

logfileName = os.path.abspath(sys.argv[0]) + '.log';
logging.basicConfig(filename = logfileName, level = logging.WARNING)
#logging.basicConfig(filename = logfileName, level = logging.DEBUG)

method = sys.argv[1]
url = sys.argv[2];
metaFile = sys.argv[3];
postFile = sys.argv[4];
dstFile = sys.argv[5];

logging.info(str(sys.argv))
try:
if url == 'https://ofx.discovercard.com':
logging.info('direct python get for discover')
in_file = open(postFile, "r")
content_bytes = in_file.read()
in_file.close()

#od = OrderedDict()
#od['Content-Type'] = 'application/x-ofx'
#od['Host'] = 'ofx.discovercard.com'
#od['Content-Length'] = str(len(content_bytes))
#od['Connection'] = 'Keep-Alive'
#s = requests.Session()
#s.headers = od
# remove default headers for some reason 'Accept-Encoding': None doesnt work
#req = requests.Request(method, url, headers = {'Accept': None, 'User-Agent': None}, data = content_bytes)
#prepped = s.prepare_request(req)
#logging.info('prepared headers %s', resp.request.headers)
#del prepped.headers['Accept-Encoding']
#resp = s.send(prepped, verify = False)
#logging.info('request headers %s', resp.request.headers)

conn = httplib.HTTPSConnection("ofx.discovercard.com")#, context = ssl._create_unverified_context())
#conn.set_debuglevel(1)
conn.connect()
conn.putrequest(method, '/', skip_host = True, skip_accept_encoding = True)
conn.putheader('Content-Type', 'application/x-ofx')
conn.putheader('Host', 'ofx.discovercard.com')
conn.putheader('Content-Length', str(len(content_bytes)))
conn.putheader('Connection', 'Keep-Alive')
conn.endheaders()
conn.send(content_bytes)
response = conn.getresponse()
respBody = response.read()

out_file = open(dstFile, "w")
out_file.write(respBody)
out_file.close()
sys.exit(0)

except Exception as e:
logging.exception("error ofx request")

sys.exit(1)

0 comments on commit 3f7acce

Please sign in to comment.