<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Vidalia: CrashReportUploader.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a> » <a class="el" href="dir_038e8b3624109884d464700e800a8093.html">crashreporter</a> </div> </div> <div class="contents"> <h1>CrashReportUploader.cpp</h1><a href="_crash_report_uploader_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment">** This file is part of Vidalia, and is subject to the license terms in the</span> <a name="l00003"></a>00003 <span class="comment">** LICENSE file, found in the top level directory of this distribution. If you</span> <a name="l00004"></a>00004 <span class="comment">** did not receive the LICENSE file with this file, you may obtain it from the</span> <a name="l00005"></a>00005 <span class="comment">** Vidalia source package distributed by the Vidalia Project at</span> <a name="l00006"></a>00006 <span class="comment">** http://www.vidalia-project.net/. No part of Vidalia, including this file,</span> <a name="l00007"></a>00007 <span class="comment">** may be copied, modified, propagated, or distributed except according to the</span> <a name="l00008"></a>00008 <span class="comment">** terms described in the LICENSE file.</span> <a name="l00009"></a>00009 <span class="comment">*/</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">/*</span> <a name="l00012"></a>00012 <span class="comment">** \file CrashReportUploader.cpp</span> <a name="l00013"></a>00013 <span class="comment">** \version $Id$</span> <a name="l00014"></a>00014 <span class="comment">** \brief Uploads a minidump file and any extra information to a crash</span> <a name="l00015"></a>00015 <span class="comment">** reporting server.</span> <a name="l00016"></a>00016 <span class="comment">*/</span> <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="_crash_report_uploader_8h.html">CrashReportUploader.h</a>"</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#include <QtGlobal></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <QDateTime></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <QSslSocket></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <QSslCertificate></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <QHttpRequestHeader></span> <a name="l00025"></a>00025 <span class="preprocessor">#include <QHttpResponseHeader></span> <a name="l00026"></a>00026 <span class="preprocessor">#include <QMap></span> <a name="l00027"></a>00027 <span class="preprocessor">#include <QUrl></span> <a name="l00028"></a>00028 <a name="l00029"></a>00029 <a name="l00030"></a><a class="code" href="class_crash_report_uploader.html#a97ab20bab5746a0b096feb8508eba300">00030</a> <a class="code" href="class_crash_report_uploader.html#a97ab20bab5746a0b096feb8508eba300">CrashReportUploader::CrashReportUploader</a>(QObject *parent) <a name="l00031"></a>00031 : QObject(parent), <a name="l00032"></a>00032 _requestId(-1) <a name="l00033"></a>00033 { <a name="l00034"></a>00034 <span class="comment">/* Clear the default CA certificate store and add the only one we want */</span> <a name="l00035"></a>00035 QSslSocket::setDefaultCaCertificates(QList<QSslCertificate>()); <a name="l00036"></a>00036 QSslSocket::addDefaultCaCertificates(<span class="stringliteral">":/pki/gd-class2-root.crt"</span>); <a name="l00037"></a>00037 <a name="l00038"></a>00038 <span class="comment">/* Create the QHttp object used to talk to the crash reporting server */</span> <a name="l00039"></a>00039 <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a> = <span class="keyword">new</span> QHttp(<span class="keyword">this</span>); <a name="l00040"></a>00040 connect(<a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>, SIGNAL(stateChanged(<span class="keywordtype">int</span>)), <a name="l00041"></a>00041 <span class="keyword">this</span>, SLOT(<a class="code" href="class_crash_report_uploader.html#a55273d86e2a702be4a8818383bf16ed7">httpStateChanged</a>(<span class="keywordtype">int</span>))); <a name="l00042"></a>00042 connect(<a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>, SIGNAL(requestFinished(<span class="keywordtype">int</span>, <span class="keywordtype">bool</span>)), <a name="l00043"></a>00043 <span class="keyword">this</span>, SLOT(<a class="code" href="class_crash_report_uploader.html#a5dbe146f036927177a59b2d15074d366">httpRequestFinished</a>(<span class="keywordtype">int</span>, <span class="keywordtype">bool</span>))); <a name="l00044"></a>00044 connect(<a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>, SIGNAL(dataSendProgress(<span class="keywordtype">int</span>, <span class="keywordtype">int</span>)), <a name="l00045"></a>00045 <span class="keyword">this</span>, SIGNAL(<a class="code" href="class_crash_report_uploader.html#a6a61859abcca3d43d1f9c67344d052a0">uploadProgress</a>(<span class="keywordtype">int</span>, <span class="keywordtype">int</span>))); <a name="l00046"></a>00046 <a name="l00047"></a>00047 <span class="comment">/* Seed the lame PRNG we'll use to generate the multipart boundary marker */</span> <a name="l00048"></a>00048 qsrand(QDateTime::currentDateTime().toTime_t()); <a name="l00049"></a>00049 } <a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="keywordtype">void</span> <a name="l00052"></a><a class="code" href="class_crash_report_uploader.html#a4a1eceee25004e7d3b18d7831a503607">00052</a> <a class="code" href="class_crash_report_uploader.html#a4a1eceee25004e7d3b18d7831a503607">CrashReportUploader::uploadMinidump</a>(<span class="keyword">const</span> QUrl &serverUrl, <a name="l00053"></a>00053 <span class="keyword">const</span> QString &minidumpId, <a name="l00054"></a>00054 <span class="keyword">const</span> QByteArray &minidump, <a name="l00055"></a>00055 <span class="keyword">const</span> QMap<QString,QString> &parameters) <a name="l00056"></a>00056 { <a name="l00057"></a>00057 QByteArray body; <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">/* Set the destination host. If it looks like the destination URL uses SSL,</span> <a name="l00060"></a>00060 <span class="comment"> * then we need to tell the QHttp object to use it as well. */</span> <a name="l00061"></a>00061 <span class="keywordflow">if</span> (! serverUrl.scheme().compare(<span class="stringliteral">"https"</span>, Qt::CaseInsensitive)) { <a name="l00062"></a>00062 <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->setHost(serverUrl.host(), QHttp::ConnectionModeHttps, <a name="l00063"></a>00063 serverUrl.port(443)); <a name="l00064"></a>00064 } <span class="keywordflow">else</span> { <a name="l00065"></a>00065 <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->setHost(serverUrl.host(), QHttp::ConnectionModeHttp, <a name="l00066"></a>00066 serverUrl.port(80)); <a name="l00067"></a>00067 } <a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="comment">/* Set up the request header */</span> <a name="l00070"></a>00070 QHttpRequestHeader header(<span class="stringliteral">"POST"</span>, serverUrl.path()); <a name="l00071"></a>00071 QString boundary = <a class="code" href="class_crash_report_uploader.html#aa7ecb1235218641eda0ff73736ad772c">generateBoundaryMarker</a>(); <a name="l00072"></a>00072 header.setValue(<span class="stringliteral">"Host"</span>, serverUrl.host()); <a name="l00073"></a>00073 header.setContentType(QString(<span class="stringliteral">"multipart/form-data; boundary=%1"</span>) <a name="l00074"></a>00074 .arg(boundary)); <a name="l00075"></a>00075 <a name="l00076"></a>00076 <span class="comment">/* Add all the key=value parameters to the request body */</span> <a name="l00077"></a>00077 <span class="keywordflow">foreach</span> (QString key, parameters.keys()) { <a name="l00078"></a>00078 QString value = parameters.value(key); <a name="l00079"></a>00079 <span class="keywordflow">if</span> (! value.isEmpty()) { <a name="l00080"></a>00080 body.append(QString(<span class="stringliteral">"--%1\r\n"</span>).arg(boundary)); <a name="l00081"></a>00081 body.append(QString(<span class="stringliteral">"Content-Disposition: form-data; name=\"%1\""</span>).arg(key)); <a name="l00082"></a>00082 body.append(<span class="stringliteral">"\r\n\r\n"</span>); <a name="l00083"></a>00083 body.append(value.toUtf8()); <a name="l00084"></a>00084 body.append(<span class="stringliteral">"\r\n"</span>); <a name="l00085"></a>00085 } <a name="l00086"></a>00086 } <a name="l00087"></a>00087 <a name="l00088"></a>00088 <span class="comment">/* Append the minidump contents */</span> <a name="l00089"></a>00089 body.append(QString(<span class="stringliteral">"--%1\r\n"</span>).arg(boundary)); <a name="l00090"></a>00090 body.append(<span class="stringliteral">"Content-Disposition: form-data; "</span>); <a name="l00091"></a>00091 body.append(<span class="stringliteral">"name=\"upload_file_minidump\"; "</span>); <a name="l00092"></a>00092 body.append(QString(<span class="stringliteral">"filename=\"%1\"\r\n"</span>).arg(minidumpId)); <a name="l00093"></a>00093 body.append(<span class="stringliteral">"Content-Type: application/octet-stream\r\n\r\n"</span>); <a name="l00094"></a>00094 body.append(minidump); <a name="l00095"></a>00095 body.append(QString(<span class="stringliteral">"\r\n--%1\r\n"</span>).arg(boundary)); <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="comment">/* Initiate the request and return the request ID */</span> <a name="l00098"></a>00098 <a class="code" href="class_crash_report_uploader.html#ad4170710f1a94f8c6cbf3d72d085afb1">_requestId</a> = <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->request(header, body); <a name="l00099"></a>00099 } <a name="l00100"></a>00100 <a name="l00101"></a>00101 QString <a name="l00102"></a><a class="code" href="class_crash_report_uploader.html#aa7ecb1235218641eda0ff73736ad772c">00102</a> <a class="code" href="class_crash_report_uploader.html#aa7ecb1235218641eda0ff73736ad772c">CrashReportUploader::generateBoundaryMarker</a>()<span class="keyword"> const</span> <a name="l00103"></a>00103 <span class="keyword"></span>{ <a name="l00104"></a>00104 <span class="keywordflow">return</span> QString(<span class="stringliteral">"%1%2"</span>).arg((quint32)qrand(), 8, 16, QChar(<span class="charliteral">'0'</span>)) <a name="l00105"></a>00105 .arg((quint32)qrand(), 8, 16, QChar(<span class="charliteral">'0'</span>)); <a name="l00106"></a>00106 } <a name="l00107"></a>00107 <a name="l00108"></a>00108 <span class="keywordtype">void</span> <a name="l00109"></a><a class="code" href="class_crash_report_uploader.html#a4490b2043602a1983ad2b684b29434f6">00109</a> <a class="code" href="class_crash_report_uploader.html#a4490b2043602a1983ad2b684b29434f6">CrashReportUploader::cancel</a>() <a name="l00110"></a>00110 { <a name="l00111"></a>00111 <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->abort(); <a name="l00112"></a>00112 } <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="keywordtype">void</span> <a name="l00115"></a><a class="code" href="class_crash_report_uploader.html#a55273d86e2a702be4a8818383bf16ed7">00115</a> <a class="code" href="class_crash_report_uploader.html#a55273d86e2a702be4a8818383bf16ed7">CrashReportUploader::httpStateChanged</a>(<span class="keywordtype">int</span> state) <a name="l00116"></a>00116 { <a name="l00117"></a>00117 <span class="keywordflow">switch</span> (state) { <a name="l00118"></a>00118 <span class="keywordflow">case</span> QHttp::Connecting: <a name="l00119"></a>00119 emit <a class="code" href="class_crash_report_uploader.html#a5ad0ec19acb32a3d15996bb5b7c93e05">statusChanged</a>(tr(<span class="stringliteral">"Connecting..."</span>)); <a name="l00120"></a>00120 <span class="keywordflow">break</span>; <a name="l00121"></a>00121 <a name="l00122"></a>00122 <span class="keywordflow">case</span> QHttp::Sending: <a name="l00123"></a>00123 emit <a class="code" href="class_crash_report_uploader.html#a5ad0ec19acb32a3d15996bb5b7c93e05">statusChanged</a>(tr(<span class="stringliteral">"Sending crash report..."</span>)); <a name="l00124"></a>00124 <span class="keywordflow">break</span>; <a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="keywordflow">case</span> QHttp::Reading: <a name="l00127"></a>00127 emit <a class="code" href="class_crash_report_uploader.html#a5ad0ec19acb32a3d15996bb5b7c93e05">statusChanged</a>(tr(<span class="stringliteral">"Receiving response..."</span>)); <a name="l00128"></a>00128 <span class="keywordflow">break</span>; <a name="l00129"></a>00129 <a name="l00130"></a>00130 <span class="keywordflow">default</span>: <a name="l00131"></a>00131 <span class="keywordflow">break</span>; <a name="l00132"></a>00132 } <a name="l00133"></a>00133 } <a name="l00134"></a>00134 <a name="l00135"></a>00135 <span class="keywordtype">void</span> <a name="l00136"></a><a class="code" href="class_crash_report_uploader.html#a5dbe146f036927177a59b2d15074d366">00136</a> <a class="code" href="class_crash_report_uploader.html#a5dbe146f036927177a59b2d15074d366">CrashReportUploader::httpRequestFinished</a>(<span class="keywordtype">int</span> <span class="keywordtype">id</span>, <span class="keywordtype">bool</span> <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">error</a>) <a name="l00137"></a>00137 { <a name="l00138"></a>00138 <span class="keywordflow">if</span> (<span class="keywordtype">id</span> != <a class="code" href="class_crash_report_uploader.html#ad4170710f1a94f8c6cbf3d72d085afb1">_requestId</a>) <a name="l00139"></a>00139 <span class="keywordflow">return</span>; <a name="l00140"></a>00140 <a name="l00141"></a>00141 <span class="keywordflow">if</span> (error) { <a name="l00142"></a>00142 QString errorString = <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->errorString(); <a name="l00143"></a>00143 emit <a class="code" href="class_crash_report_uploader.html#a642526a84084ab0b76eebf0740ed58b7">uploadFailed</a>(errorString); <a name="l00144"></a>00144 } <span class="keywordflow">else</span> { <a name="l00145"></a>00145 QHttpResponseHeader response = <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->lastResponse(); <a name="l00146"></a>00146 <span class="keywordflow">if</span> (response.statusCode() == 200) <a name="l00147"></a>00147 emit <a class="code" href="class_crash_report_uploader.html#aa4c3ad140d01282f3259733dbedde045">uploadFinished</a>(); <a name="l00148"></a>00148 <span class="keywordflow">else</span> <a name="l00149"></a>00149 emit <a class="code" href="class_crash_report_uploader.html#a642526a84084ab0b76eebf0740ed58b7">uploadFailed</a>(response.reasonPhrase()); <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <a class="code" href="class_crash_report_uploader.html#a018ef59ab349a19e598f0fc2539f068d">_http</a>->close(); <a name="l00152"></a>00152 } <a name="l00153"></a>00153 </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:54 2010 for Vidalia by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>