Im working with on pop3 clinet using openSSL library. However I run to this incosistency problem, when reading from BIO socket, 4 times out of 5 there is "\r\n.\r\n" at the end, indicating end of multi-line response from server. But sometimes only "\r\n" is being returned for some reason, and the rest ".\r\n" appears in the next BIO_read. Buffer is not overflowing, it is set to 1024 length and this happens even in 604 characters long response.
Example:
+OK 555 octets
Return-Path: test1#mail.local
Received: from [127.0.0.1] (mail.local [127.0.0.1])
by LAPTOP-R30GIL79 with ESMTPA
; Wed, 20 Oct 2021 11:55:24 +0200
Message-ID: <dad24da8-dd91-62ed-3338-95fe46890c32#mail.local>
Date: Wed, 20 Oct 2021 11:55:24 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.0
Content-Language: sk
To: test1#mail.local
From: test1 <test1#mail.local>
Subject: testing
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Text testovacej spravy.
.
calling it exactly same after that results in:
+OK 555 octets
Return-Path: test1#mail.local
Received: from [127.0.0.1] (mail.local [127.0.0.1])
by LAPTOP-R30GIL79 with ESMTPA
; Wed, 20 Oct 2021 11:55:24 +0200
Message-ID: <dad24da8-dd91-62ed-3338-95fe46890c32#mail.local>
Date: Wed, 20 Oct 2021 11:55:24 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.2.0
Content-Language: sk
To: test1#mail.local
From: test1 <test1#mail.local>
Subject: testing
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Text testovacej spravy.
\r\n
As you can see ".\r\n" is missing for some reason. Octets does match but it disappear somehow. There are no memory leaks. This is function Im using for reading BIO socket
int readBIO(){
memset(buf, 0, strlen(buf));
int x = BIO_read(bio,buf,BUFF_SIZE);
if (x == 0 )
{
fprintf(stderr, "Error: Trying to read from closed connection.\n");
freeAll();
exit(1);
}else if(x < 0){
if (! BIO_should_retry(bio))
{
fprintf(stderr, "Error: Reading from bio socked has failed.\n");
freeAll();
exit(1);
}
}
if (checkResponse() != 0)
{
fprintf(stderr, "Error: Incorrect command / typo in command detected.\n Or wrong username / password has been entered.\n Server responded with -ERR.\n");
freeAll();
exit(1);
}
return 0;
}
Anyone might have an idea why this is happening?
I'm trying to upload file on one of my Azure containers
this is one of my request send with ajax:
headers: Object
Authorization: "SharedKey MYACCOUNT:ENC_KEY"
Content-Type: "application/octet-stream"
data: File
x-ms-blob-type: "BlockBlob"
x-ms-date: "Mon, 19 Oct 2015 13:54:53 GMT"
x-ms-version: "2009-09-19"
type: "PUT"
url: "https://MYACCOUNT.blob.core.windows.net/data-test"
for the ENC_KEY I use :
authorizationHeader =
compute: (options, xhrOptions) ->
sig = #_computeSignature(options, xhrOptions)
result = 'SharedKey ' + options.storageAccount + ':' + sig
result
_computeSignature: (options, xhrOptions) ->
sigString = #_getSignatureString(options, xhrOptions)
key = CryptoJS.enc.Base64.parse(options.primaryKey)
hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key)
hmac.update sigString
hash = hmac.finalize()
result = hash.toString(CryptoJS.enc.Base64)
result
any ideas?
EDIT :
all code for authorizationHeader ->
https://gist.github.com/F4Ke/88debcede3b7e2312b11
2)
ERROR RESPONSE :
PUT https://MYACCOUNT.blob.core.windows.net/data-test 403 (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.)
3)
#_getCanonicalizedHeadersString
itemcreation.coffee:232 x-ms-blob-type:BlockBlob
x-ms-date:Mon, 19 Oct 2015 14:33:15 GMT
x-ms-version:2009-09-19
#_getSignatureString
PUT
application/octet-stream
x-ms-blob-type:BlockBlob
x-ms-date:Mon, 19 Oct 2015 14:35:19 GMT
x-ms-version:2009-09-19
/MYACCOUNT/MYACCOUNT.blob.core.windows.net/data-test
The REST documentation describing how to sign a message can be found here. You can also take a look at the node shared key implementation hhere - might also help you...
I have a little web server in C which works fine when it has to serve html or css files. But when it comes to non-text files (image, compressed files, etc.), the data are corrupted when received by the client, which can't display them.
The response from the server is like this :
HTTP/1.1 200 OK
Date: Wed, 11 Jun 2014 11:24:27 GMT
Server: MacFly/0.1.1
Cache-Control: max-age=60
Expires: Wed, 11 Jun 2014 11:25:27 GMT
Last-Modified: Wed, 11 Jun 2014 06:13:38 GMT
ETag: "1041463782"
Content-Length: 36851
Content-Type: image/jpeg
���
The last line correspond to the corrupted data.
To generate the data, I use the following function which read the demanded file (any format : html, css, jpg, etc.) and save it in file_contents :
void get_content(const char * filename, char * file_contents) {
FILE * file;
int file_size;
file_size = get_file_size(filename);
if (file = fopen(filename, "rb")) {
fread(file_contents, sizeof(char), file_size-1, file);
fclose(file);
}
}
I am using curl library for email sending.
I am able to send email successfully. I can see FROM, Subject and body properly at receiving end.
Now my need is for attachment in the email.
I have followed in this way:
http://msdn.microsoft.com/en-us/library/ms526560(v=exchg.10).aspx
static const char *text[]={
"Date: Wed, 06 Feb 2013 12:30:30 +1100\n",
"To: " RECIPIENT "\n",
"From: " MAILFROM "\n",
"MIME-Version: 1.0",
"Content-Type: multipart/mixed",
"boundary=""XXXXX""\n",
"Subject: email example message\n",
"--XXXXX",
"\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\n",
"Check RFC5322.\n",
"--XXXXX",
"Content-Type: text/plain" "\n",
"Content-Disposition: attachment" "\n",
"filename="FILE_PATH "\n",
"--XXXXX--",
NULL
};
They are suggesting RFC5322. But in that I could not find anything like attachment.
Is it correct? Any other way using libcurl + C language is also welcomed.
I think the person named as Jorge has asked same kind of question on october 13,2012. But the answer is not there.
The easiest way is to encode using something like base64 encoding which turns binary data into ASCII characters that are able to be sent via email. base64 encode the file and insert the string as below. Replace everything in caps with the correct stuff.
--XXXXboundary text
Content-Type: application/FILETYPE; name="FILENAME"
Content-Disposition: attachment; filename="FILENAME"
Content-Transfer-Encoding: base64
STRINGFROMBASE64ENCODING
Heres a thread on base64: How do I base64 encode (decode) in C?
I think you are right, but why did you attached a null file? I think you need put something in the final attachment body apartment.
For example:
--XXXXboundary text
Content-Type: text/plain;
Content-Disposition: attachment;
filename="test.txt"
this is the attachment text ---- this is attachment content
--XXXXboundary text--
Maybe you should take a look at libquickmail (http://sf.net/p/libquickmail/).
It does all the magic needed to attach files and it uses libcurl for the SMTP transport (or even without libcurl in libquickmaillight).
It can be done as below;
static const char *payload_text[] = {
"To: " TO "\r\n",
"From: " FROM "(Example User)\r\n",
"Cc: " CC "(Another example User)\r\n",
"Subject: SMTPS Example\r\n",
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"User-Agent: My eMail Client\r\n",
"MIME-Version: 1.0\r\n",
"Content-Type: multipart/mixed;\r\n",
" boundary=\"------------030203080101020302070708\"\r\n",
"\r\nThis is a multi-part message in MIME format.\r\n",
"--------------030203080101020302070708\r\n",
"Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
"Content-Transfer-Encoding: 7bit\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\r\n",
"\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\r\n\r\n",
"--------------030203080101020302070708\r\n",
"Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
" name=\"send.c\"\r\n",
"Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
"Content-Disposition: attachment;\r\n",
" filename=\"abc.txt\"\r\n",
"\r\n",
"bla bla file content is here\r\n",
"--------------030203080101020302070708--\r\n",
NULL
};
i am trying to call this rest api method
#HttpGet
global static String retrievingNotificationSettings(){
RestRequest req=RestContext.request;
RestResponse res=RestContext.response;
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
if(req.headers.get('Content-Type').equals('application/xml'))
{
Map<String, SObjectField> fields = Notification_Settings__c.SObjectType.getDescribe().fields.getMap();
Schema.sObjectField T ;
Notification_Settings__c ss;
return 'hello World';
}
if(req.headers.get('Content-Type').equals('application/json'))
return System.JSON.serialize(note);
return null;
My request is
GET /services/apexrest/v.9/notifications/preferences/ritesh HTTP/1.1
X-HostCommonName:
ap1.salesforce.com
Authorization:
OAuth 00D90000000j9AW!AQkAQLba73cDzjXhQ4kkQ2PSru4XpFuJcwr5kg_W_MkZmQnm9vI653FBWeJaABwClQqtJZD_b6j7V0O_elkzvkh7IqRKSUop
X-PrettyPrint:
1
Host:ap1.salesforce.com
X-Target-URI:
https://ap1.salesforce.com
Content-Type: application/json
Connection:Keep-Alive
and the response i am getting is
HTTP/1.1 500 Internal Server Error
Date:
Fri, 25 Jan 2013 16:30:21 GMT
Content-Length:
203
Connection:
close
Content-Type:
application/json; charset=UTF-8
Server:
[
{
"message": "System.NullPointerException: Attempt to de-reference a null object\n\nClass.NotificationRestService.retrievingNotificationSettings: line 34, column 1",
"errorCode": "APEX_ERROR"
}
]
the line where i am getting error is
if(req.headers.get('Content-Type').equals('application/xml'))
i didn't understand why i am getting this error when in header i am passing value Content-Type as application/json ?? please tell me where i am making mistake
Can you System.debug(req.headers) to see if it was passed correctly?
To answer your null pointer question literally - use == instead of equals:
if(req.headers.get('Content-Type') == 'application/xml')
but that won't solve the real underlying problem.
You might want to try with different header (stupid, I know) or decide to add a GET parameter, something like &mode=json