What if NATS message payload contains \n\r? - nats.io

I am trying to write a client library for NATS.io. According to the protocol here, \n\r is used to delimiting commands, payload, etc.
INFO {"server_id":"1ec445b504f4edfb4cf7927c707dd717","version":"0.6.6","go":"go1.4.2","host":"0.0.0.0","port":4222,"auth_required":false,"ssl_required":false,"max_payload":1048576}
My question is what if the payload contains \r\n? I couldn't find any information about how to escape \r\n. Should one read INFO until a valid JSON is received and not look for \r\n as delimiter?
Thanks for your time!

A subscribe message MSG always contains the length of the payload bytes.
docu
Example
MSG FOO.BAR 9 11\r\nHello World\r\n
The payload are the 11 bytes after the first\r\n ---> Hello World
Example with \r\n in the payload
MSG FOO.BAR 9 11\r\nHello W\r\nld\r\n
The payload are the 11 bytes after the first\r\n ---> Hello W\r\nld

Related

Foreign character when send file image using http via multipart / form-data

I'm trying to understand the http request, when I send a string of data using the post method on the request body there is a value that we send as below:
--------------------------d74496d66958873e
Content-Disposition: form-data; name="person"
anonymous
--------------------------d74496d66958873e
but if we send a file using post method it will be like this:
--------------------------d74496d66958873e
Content-Disposition: form-data; name="fileToUpload"; filename="icon.png"
Content-Type: image/png
-O9†q#ë#ÞÿËà3l†v}uá#t(<‡c3f
úS©59ñõCáa#Ž¡#Za%ð.ž zxý˜F#ZqÄð&^
jx[1…ÕЊËÂ$Æ‚#Þ
--------------------------d74496d66958873e
my question is:
what is the foreign character contained between -------------------------- d74496d66958873e when we send the file?
i mean
-O9†q#ë#ÞÿËà3l†v}uá#t(<‡c3f
úS©59ñõCáa#Ž¡#Za%ð.ž zxý˜F#ZqÄð&^
jx[1…ÕЊËÂ$Æ‚#Þ
whether the character is a binary, hexa, or base64 or what?
how to change image file to the character when we want to write http request manually using programming language?
Those are plain binary bytes. All the bytes that the file icon.png contained when it was sent by the HTTP client.
The format is described in RFC 1867 and basically works like this:
--[boundary]
[headers]
[N bytes]
--[boundary]
[headers]
[M bytes]
--[boundary]--
To extract the contents, you'll need to parse past the boundary, parse the headers (0, 1 or many) and then read the binary data until you reach the ending boundary. (The final boundary has two extra dashes on the right.)
... and there can be any amount of such parts, in a single multipart POST.

how to read mavlink package .. is it ok to do it on my way?

i reading about MAVlink and i try to read packages from pixhawk fly controller.
I thought of another way to make the call and i want to know from this discussions readers if its ok and what are you think
On my reader .. i read the two first byte from the pixhawk.
The second byte need to be the PAYLOAD length --> so new i know that i need to read the 4 bytes of the header + PAYLOAD length bytes + 2 chcksub bytes.
So after reading the PAYLOAD length i define a byte array -> size is
( PAYLOAD.length + 4 + 2 ) and read from the serial to this buffer.
Is it ok to do it ?
The MAVLink protocol has HEADER + PAYLOAD + SIGNATURE format.
MAVLink v1.0
v1.0 is the standard protocol as specified by QGroundControl. It has the format:
6 bytes header
(PAYLOAD length) bytes payload
2 bytes signature (checksum)
The first byte is always the protocol start-of-frame: 0xFE
The second byte is always the payload length.
Hence your receive buffer size should be (PAYLOAD length) + 8.
The method you described will generally work for most packets received from the pixhawk. However, pixhawk (ArduPilot) makes use of an extended MAVLink protocol which has been coined "v2.0" which adds additional header and signature bytes.
MAVLink v2.0
v2.0 is the extended protocol which applies to a select few messages such as "STATUSTEXT". It has the format:
10 bytes header
(PAYLOAD length) bytes payload
15 bytes signature
The first byte has the start-of-frame: 0xFD
The second byte is again the payload length.
Hence, your buffer size should be (PAYLOAD length) + 25.
If you want to process MAVLink message data from the pixhawk, or from the generated .tlog file you should set your input message buffer size based on the start of frame and payload length bytes, the first two bytes of any MAVLink message.

What is the difference between a request payload and request body?

I am learning HTTP. I enclose a request payload in XML or JSON format in my POST requests. What I wanted to know is whether a request payload and request body mean the same thing?
Definition of: payload : The "actual data" in a packet or file minus all headers attached for transport and minus all descriptive meta-data. In a network packet, headers are appended to the payload for transport and then discarded at their destination.
Edit: In Http protocol, an http packet has http headers and http payload.So payload section of http packet may or may not have a body depending upon the type of request (e.g. POST vs GET). So payload and body are not the same thing.
Payload is the "wrapper" to the body
Payload is something one carries. A paperboy's payload is a pile of newspapers and a HTTP POST request's payload is whatever comes in the "body".
What I wanted to know is whether a request payload and request body mean the same thing?
No, they have different meanings. A payload (a.k.a. content) is a part of representation data while a body is a part of a message, which are two different HTTP concepts. A representation (data and metadata) is transferred as a single or multiple messages, so a message encloses a complete or partial representation. The representation metadata are enclosed in the header fields of a message and the representation data, the payload, are enclosed in the body of a message, as is or transfer-encoded.
References
RFC 9110: HTTP Semantics defines the term representation:
3.2. Representations
A "representation" is information that is intended to reflect a past, current, or desired state of a given resource, in a format that can be readily communicated via the protocol. A representation consists of a set of representation metadata and a potentially unbounded stream of representation data (Section 8).
Notice that the definition is independent of the version of HTTP because it is about semantics.
RFC 9112: HTTP/1.1 defines the term message:
2.1. Message Format
An HTTP/1.1 message consists of a start-line followed by a CRLF and a sequence of octets in a format similar to the Internet Message Format [RFC5322]: zero or more header field lines (collectively referred to as the "headers" or the "header section"), an empty line indicating the end of the header section, and an optional message body.
HTTP-message = start-line CRLF
*( field-line CRLF )
CRLF
[ message-body ]
Notice that the definition depends on the version of HTTP because it is about syntax.
RFC 9110: HTTP Semantics defines the term content:
6.4. Content
HTTP messages often transfer a complete or partial representation as the message "content": a stream of octets sent after the header section, as delineated by the message framing.
This abstract definition of content reflects the data after it has been extracted from the message framing. For example, an HTTP/1.1 message body (Section 6 of [HTTP/1.1]) might consist of a stream of data encoded with the chunked transfer coding -- a sequence of data chunks, one zero-length chunk, and a trailer section -- whereas the content of that same message includes only the data stream after the transfer coding has been decoded; it does not include the chunk lengths, chunked framing syntax, nor the trailer fields (Section 6.5).
Note: Some field names have a "Content-" prefix. This is an informal convention; while some of these fields refer to the content of the message, as defined above, others are scoped to the selected representation (Section 3.2). See the individual field's definition to disambiguate.
RFC 9110: HTTP Semantics substitutes the term content for payload used in previous RFCs:
B.3. Changes from RFC 7231
[…]
The terms "payload" and "payload body" have been replaced with "content", to better align with its usage elsewhere (e.g., in field names) and to avoid confusion with frame payloads in HTTP/2 and HTTP/3. (Section 6.4)
Header identifies source & destination of the sent packet, whereas the actual data i.e Body is referred to as Payload
The start-line and HTTP headers of the HTTP message are collectively known as the head of the requests, whereas its payload is known as the body
So Yes, they are the same thing.
Got this from https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
Payload of HTTP message is known as the body. link1
The HTTP message payload body is the information ("payload") part of the data that is sent in the HTTP Message Body (if any), prior to transfer encoding being applied. If transfer encoding is not used, the payload body and message body are the same thing! link2
So basically the only difference between HTTP message body and HTTP message payload body is encoding (but only if present). So generalizing the term request payload = request body.

How the "OK" message looks like?

I have a device that sends data to a server.
Data
[ Client ] == > [ Server ]
After the validation on the server I want to return a message:
OK
[ Client ] < == [ Server ]
Is there a standard "OK" message to return? And an "ERROR" message? How does it looks like? (e.g. ":0011", ":110F")
You've got to design an application-level protocol. TCP is a byte stream, so even the definition of "Data" in your client->server piece needs some protocol so that the receiver can know what bytes make up the data (when to stop reading).
A couple of common types of protocols are...
Length-delimited chunks. Every message starts with a 16 or 32-bit length prefix. Then that many bytes follow. The length needs to be in a defined byte order (see htons, ntohs, etc). Everyone who uses this protocol knows to read the length prefix then read that many bytes. Having defined that "chunk" on the network, you might put a header on the contents of the chunk. Maybe a message type (ACK, NAK, Data, etc) followed by some contents.
ASCII newline delimited. Each message is a line of ASCII (or UTF8, etc) text. It ends at a newline. Newline endings for the lines play the same role as the length prefix for the chunks above. You then define what's in each line (like space or comma-delimited ASCII/UTF8/whatever fields). Somewhere in that you'd define what data looks like, ACK, etc.
I'm sure you could come up with other ideas, but that's the basic job: defining your application-level protocol on top of TCP's byte stream.

Question about POP3 message termination octet

This is from the POP3 RFC.
"Responses to certain commands are multi-line. In these cases, which
are clearly indicated below, after sending the first line of the
response and a CRLF, any additional lines are sent, each terminated
by a CRLF pair. When all lines of the response have been sent, a
final line is sent, consisting of a termination octet (decimal code
046, ".") and a CRLF pair. If any line of the multi-line response
begins with the termination octet, the line is "byte-stuffed" by
pre-pending the termination octet to that line of the response.
Hence a multi-line response is terminated with the five octets
"CRLF.CRLF". When examining a multi-line response, the client checks
to see if the line begins with the termination octet. If so and if
octets other than CRLF follow, the first octet of the line (the
termination octet) is stripped away. If so and if CRLF immediately
follows the termination character, then the response from the POP
server is ended and the line containing ".CRLF" is not considered
part of the multi-line response."
Well, i have problem with this, for example gmail sometimes sends the termination octet and then in the NEXT LINE sends the CRLF pair. For example:
"+OK blah blah\r\n"
"blah blah.\r\n"
"\r\n"
That's very rare, but it happens sometimes, so obviously i'm unable to determine the end of the message in such case, because i'm expecting a line that consists of '.\r\n'. Seriously, is Gmail violating the POP3 protocol or i'm doing something wrong? Also i have a second question, english is not my first language so i cannot understand that completely:
"If any line of the multi-line response begins with the termination octet, the line is "byte-stuffed" by pre-pending the termination octet to that line of the response.
Hence a multi-line response is terminated with the five octets "CRLF.CRLF"."
When exactly CRLF.CRLF is used? Can someone gives me a simple example? The rfc says that is used when any line of the response begins with the termination octet. But i don't see any lines that starts with '.' in the messages that are terminated with CRLF.CRLF. I checked that. Maybe i don't understand something, that's why i'm asking.
It would help very much if you posted the code you are using to read the socket. But I will attempt to answer the second part of your question with this example:
If the response is:
hello world\r\n
we are doing fine\r\n
.500 is same as one-half\r\n
this is the last line\r\n
the server must send it as:
hello world\r\n
we are doing fine\r\n
..500 is same as one-half\r\n
this is the last line\r\n
.\r\n
So you can see where it 'byte stuffed' an extra '.' so the '.500' can be distinguished as part of the response. Also the final five octets are '\r\n.\r\n'.

Resources