From 2e96c3880c010e34363be9300ebbc7935de4a67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Sch=C3=BCmann?= Date: Fri, 11 Apr 2014 17:22:27 +0000 Subject: SendSS: + CSend now handles image upload via HTTP, at least provides helper functions for POST requests (CSend) - CSendImageShack simplified and removed custom HTTP form stuff as it's now integrated and easier to use in CSend (CSendImageShack) * version bump and User-Agent fix (version.h) git-svn-id: http://svn.miranda-ng.org/main/trunk@8958 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/SendScreenshotPlus/src/CSendImageShack.cpp | 244 ++++----------------- 1 file changed, 41 insertions(+), 203 deletions(-) (limited to 'plugins/SendScreenshotPlus/src/CSendImageShack.cpp') diff --git a/plugins/SendScreenshotPlus/src/CSendImageShack.cpp b/plugins/SendScreenshotPlus/src/CSendImageShack.cpp index 84c57e0274..f6141651c2 100644 --- a/plugins/SendScreenshotPlus/src/CSendImageShack.cpp +++ b/plugins/SendScreenshotPlus/src/CSendImageShack.cpp @@ -34,19 +34,9 @@ CSendImageShack::CSendImageShack(HWND Owner, MCONTACT hContact, bool bAsync) : CSend(Owner, hContact, bAsync) { m_EnableItem = SS_DLG_DESCRIPTION | SS_DLG_AUTOSEND | SS_DLG_DELETEAFTERSSEND; m_pszSendTyp = LPGENT("Image upload"); - m_pszFileName = NULL; - m_pszContentType = NULL; - m_MFDRboundary = NULL; - m_nlreply = NULL; - m_Url = NULL; } CSendImageShack::~CSendImageShack(){ - mir_free(m_pszFileName); - mir_free(m_MFDRboundary); - // FREEHTTPREQUESTSTRUCT* - if (m_nlreply) CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM) m_nlreply); - mir_free(m_Url); }; //--------------------------------------------------------------------------- @@ -56,123 +46,19 @@ int CSendImageShack::Send() { Exit(ACKRESULT_FAILED); return !m_bAsync; } - if (!m_pszFileName) { - m_pszFileName = GetFileNameA(m_pszFile); - } - if (!m_pszContentType) GetContentType(); - - // create new boundary - MFDR_Reset(); - - // initialize the netlib request ZeroMemory(&m_nlhr, sizeof(m_nlhr)); - m_nlhr.cbSize = sizeof(m_nlhr); - m_nlhr.requestType = REQUEST_POST; - m_nlhr.flags = NLHRF_HTTP11; //NLHRF_DUMPASTEXT; - m_nlhr.szUrl = "http://www.imageshack.us/upload_api.php"; - m_nlhr.headersCount = 6; - { //NETLIBHTTPHEADER start - m_nlhr.headers=(NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*m_nlhr.headersCount); - m_nlhr.headers[0].szName = "Referer"; - m_nlhr.headers[0].szValue = "http://www.imageshack.us/upload_api.php"; - m_nlhr.headers[1].szName = "Connection"; - m_nlhr.headers[1].szValue = "Keep-alive"; - m_nlhr.headers[2].szName = "AcceptLanguage"; - m_nlhr.headers[2].szValue = "en-us, pt-br"; - m_nlhr.headers[3].szName = "Host"; - m_nlhr.headers[3].szValue = "imageshack.us"; - m_nlhr.headers[4].szName = "User-Agent"; - m_nlhr.headers[4].szValue = __USER_AGENT_STRING; //szAgent; /; - //nlhr.headers[x].szName = "Authorization"; - //nlhr.headers[x].szValue = auth; //Basic base-64-authorization - - //$header .= "Content-type: multipart/form-data; boundary=" . part::getBoundary() . "\r\n"; - mir_snprintf(m_nlheader_ContentType, SIZEOF(m_nlheader_ContentType), "multipart/form-data; boundary=%s", m_MFDRboundary); - m_nlhr.headers[m_nlhr.headersCount-1].szName = "Content-Type"; - m_nlhr.headers[m_nlhr.headersCount-1].szValue = m_nlheader_ContentType; - } //NETLIBHTTPHEADER end - -//POST DATA file-header, init DATA with MultipartFormDataRequest - //$params[] = new filepart('fileupload', $file, basename($file), $contentType, 'iso-8859-1'); - //($this->sendStart($h);) - AppendToData("--"); - AppendToData(m_MFDRboundary); - AppendToData("\r\n"); - //($this->sendDispositionHeader($h);) - AppendToData("Content-Disposition: form-data; name=\""); - AppendToData("fileupload"); - AppendToData("\"; filename=\""); - AppendToData(m_pszFileName); - AppendToData("\""); - AppendToData("\r\n"); - //($this->sendContentTypeHeader($h);) - AppendToData("Content-Type: "); - AppendToData(m_pszContentType); - AppendToData("; charset="); - AppendToData("iso-8859-1"); - //($this->sendEndOfHeader($h);) - AppendToData("\r\n"); - AppendToData("\r\n"); - //Now we add the file binary ($this->sendData($h)) - FILE * fileId = _tfsopen(m_pszFile, _T("rb"), _SH_DENYWR ); - if( !fileId) { - Error(SS_ERR_INIT, m_pszSendTyp); - Exit(ACKRESULT_FAILED); + char* tmp; tmp=mir_t2a(m_pszFile); + HTTPFormData frm[]={ + {"fileupload",tmp,HTTPFORM_FILE}, + //{"rembar","yes"},// no info bar on thumb + {"public","no"}, + {"key",DEVKEY_IMAGESHACK,HTTPFORM_8BIT}, + }; + int error=HTTPFormCreate(&m_nlhr,REQUEST_POST,"http://imageshack.us/upload_api.php",frm,sizeof(frm)/sizeof(HTTPFormData)); + mir_free(tmp); + if(error) return !m_bAsync; - } - fseek(fileId, NULL, SEEK_END); - size_t lenFile = ftell(fileId); - size_t sizeDest = sizeof(char)*(m_nlhr.dataLength + lenFile + 1); - m_nlhr.pData = (char *) mir_realloc(m_nlhr.pData, sizeDest); - fseek(fileId, NULL, SEEK_SET ); - int i; - int ch = fgetc( fileId ); - for( i=0; (i < (int)lenFile ) && ( feof( fileId ) == 0 ); i++ ) { - m_nlhr.pData[m_nlhr.dataLength+i] = (char)ch; - ch = fgetc( fileId ); - } - m_nlhr.pData[sizeDest-1] = 0; //NULL Termination for binary data - m_nlhr.dataLength = (int)sizeDest - 1; - fclose(fileId); - //($this->sendEnd($h);) - AppendToData("\r\n"); - -//POST DATA footer (for "optimage", 1) -//POST DATA footer (for "optsize", optsize) - -//POST DATA footer (for "tags", tags) -//POST DATA footer (for "rembar", "yes" : "no") -//POST DATA footer (for "public", "yes" : "no") -//POST DATA footer (for "cookie", cookie) - -//POST DATA footer (for "key", DEVKEY_IMAGESHACK) - //($this->sendStart($h);) - AppendToData("--"); - AppendToData(m_MFDRboundary); - AppendToData("\r\n"); - //($this->sendDispositionHeader($h);) - AppendToData("Content-Disposition: form-data; name=\""); - AppendToData("key"); - AppendToData("\""); - //($this->sendTransferEncodingHeader($h); ) - AppendToData("\r\n"); - AppendToData("Content-Transfer-Encoding: "); - AppendToData("8bit"); //??"binary" - //($this->sendEndOfHeader($h);) - AppendToData("\r\n"); - AppendToData("\r\n"); - //($this->sendData($h);) - AppendToData(DEVKEY_IMAGESHACK); - //($this->sendEnd($h);) - AppendToData("\r\n"); - -//POST DATA Exit - //$postdata = "--" . part::getBoundary() . "--\r\n"; - AppendToData("--"); - AppendToData(m_MFDRboundary); - AppendToData("--\r\n"); - -//start upload thread + /// start upload thread if(m_bAsync){ mir_forkthread(&CSendImageShack::SendThreadWrapper, this); return 0; @@ -183,24 +69,35 @@ int CSendImageShack::Send() { void CSendImageShack::SendThread() { //send DATA and wait for m_nlreply - m_nlreply = (NETLIBHTTPREQUEST *) CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM) hNetlibUser, (LPARAM) &m_nlhr); - mir_freeAndNil(m_nlhr.pData); - mir_freeAndNil(m_nlhr.headers); - if(m_nlreply){ - if( m_nlreply->resultCode >= 200 && m_nlreply->resultCode < 300 ){ - m_nlreply->pData[m_nlreply->dataLength] = 0;// make sure its null terminated - const char* URL = NULL; - URL = GetTagContent(m_nlreply->pData, "", ""); - if (URL && URL[0]!= NULL) { - m_Url = mir_strdup(URL); - if(m_bSilent) - return; - svcSendMsgExit(URL); return; - }else{//check error mess from server - TCHAR* err = mir_a2t(GetTagContent(m_nlreply->pData, "")); - if (!err || !*err){//fallback to server response mess - mir_freeAndNil(err); - err = mir_a2t(m_nlreply->pData); + NETLIBHTTPREQUEST* reply=(NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&m_nlhr); + HTTPFormDestroy(&m_nlhr); + if(reply){ + if(reply->resultCode>=200 && reply->resultCode<300){ + reply->pData[reply->dataLength]='\0';/// make sure its null terminated + const char* url=NULL; + url=GetHTMLContent(reply->pData,"",""); + if(url && *url){ + mir_free(m_URL), m_URL=mir_strdup(url); + mir_free(m_URLthumb), m_URLthumb=mir_strdup(m_URL); + int extlen; + char* pos=strrchr(m_URLthumb,'.'); + if(pos && (extlen=mir_strlen(pos))>2){ + char* tmp=mir_strdup(pos); + memcpy(pos,".th",3); + memcpy(pos+3,tmp,extlen-3); + mir_stradd(m_URLthumb,tmp+extlen-3); + mir_free(tmp); + }else{ + mir_freeAndNil(m_URLthumb); + } + svcSendMsgExit(url); return; + }else{/// check error mess from server + url=GetHTMLContent(reply->pData,""); + TCHAR* err=NULL; + if(url) err=mir_a2t(url); + if (!err || !*err){/// fallback to server response mess + mir_free(err); + err=mir_a2t(reply->pData); } Error(_T("%s"),err); mir_free(err); @@ -208,8 +105,7 @@ void CSendImageShack::SendThread() { }else{ Error(LPGENT("Upload server did not respond timely.")); } - CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM) m_nlreply); - m_nlreply = NULL; + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,(LPARAM)reply); }else{ Error(SS_ERR_INIT, m_pszSendTyp); } @@ -219,61 +115,3 @@ void CSendImageShack::SendThread() { void CSendImageShack::SendThreadWrapper(void * Obj) { reinterpret_cast(Obj)->SendThread(); } - -//--------------------------------------------------------------------------- -void CSendImageShack::MFDR_Reset() { - char Temp[64]; - DWORD dwBoundaryRand1 = GetTickCount(); - DWORD dwBoundaryRand2 = rand(); - - mir_freeAndNil(m_MFDRboundary); - mir_snprintf(Temp, SIZEOF(Temp), "B-O-U-N-D-A-R-Y%u%u", dwBoundaryRand1, dwBoundaryRand2); - mir_stradd(m_MFDRboundary,Temp); -} - -void CSendImageShack::GetContentType() { - if (m_pszContentType) mir_freeAndNil(m_pszContentType); - char* FileExtension = GetFileExtA(m_pszFile); - - if ((strcmp(FileExtension, ".jpeg")==0) || (strcmp(FileExtension, ".jpe")==0) || (strcmp(FileExtension ,".jpg")==0)) - m_pszContentType="image/jpeg"; - else if (strcmp(FileExtension, ".bmp")==0) - m_pszContentType="image/bmp"; - else if (strcmp(FileExtension, ".png")==0) - m_pszContentType="image/png"; - else if (strcmp(FileExtension, ".gif")==0) - m_pszContentType="image/gif"; - else if ((strcmp(FileExtension, ".tif")==0) || (strcmp(FileExtension, ".tiff")==0)) - m_pszContentType="image/tiff"; - else - m_pszContentType="application/octet-stream"; - - mir_free(FileExtension); - return; -} - -void CSendImageShack::AppendToData(const char *pszVal) { - if (!m_nlhr.pData) { - m_nlhr.pData = mir_strdup(pszVal); - m_nlhr.dataLength = (int)strlen(pszVal); - } - else { - size_t lenVal = strlen(pszVal); - size_t sizeNew = sizeof(char)*(m_nlhr.dataLength + lenVal + 1); - m_nlhr.pData = (char*) mir_realloc(m_nlhr.pData, sizeNew); - - strcpy(m_nlhr.pData + sizeof(char)*m_nlhr.dataLength, pszVal); - m_nlhr.pData[sizeNew-1] = 0; - m_nlhr.dataLength = (int)sizeNew -1; - } -} - -//--------------------------------------------------------------------------- -const char * CSendImageShack::GetTagContent(char * pszSource, const char * pszTagStart, const char * pszTagEnd) { - char * b = strstr(pszSource, pszTagStart); - if (!b) return NULL; - b += strlen(pszTagStart); - char * e = strstr(b, pszTagEnd); - if (e) *e = 0; - return b; -} -- cgit v1.2.3