Can I pushback messages in my class which extends Spring Integrations ByteArrayLengthHeaderSerializer? - tcp

I am extending the ByteArrayLengthHeaderSerializer to return the length from a tcp message header. The problem is the very first message on the socket contains an 8 byte session without a header. After that first message, all messages will have a header with a length (as well as some other fields). The first 4 bytes of the header will always be a constant value.
I'd like to read the first 4 bytes to determine if I have a message with a header or a raw sessionId.
If not a header then I would push back the 4 bytes and return a length of 8.
If it was a header (first 4 bytes matched the constant value) then I would read the rest of the header, find the length field in the header and return that value.
Also, this application is probably using nio.

Not directly; I did something similar a few years ago by writing a custom deserializer that used a PushBackInputStream.
You should be able to write a custom deserializer; wrap the IS in an PushBackIS. Peek at the start and push back if needed, then delegate to the BALHS to decode those messages with proper headers.

Related

What byte range 0- means

What "Range: bytes=0-" header means ? Is the entire file ? I tried sending back 0 bytes and is not working, when I send the entire file it works, but I receive this request more than once in a streaming context, it doesn't look right.
Is the entire file ?
Yes, exactly that.
The spec has the grammar:
byte-range-set = 1#( byte-range-spec / suffix-byte-range-spec )
byte-range-spec = first-byte-pos "-" [ last-byte-pos ]
and also notes:
If the last-byte-pos value is
absent, or if the value is greater than or equal to the current
length of the representation data, the byte range is interpreted as
the remainder of the representation
Additionally:
A client can request the last N bytes of the selected representation
using a suffix-byte-range-spec.
suffix-byte-range-spec = "-" suffix-length
So, valid examples from the spec include:
bytes=-500
bytes=9500-
bytes=0-0,-1
I receive this request more than once in a streaming context
The header indicates that this client understands range requests, and would accept a 206 Partial Content response, rather than the entire file, for efficient streaming (What does the HTTP 206 Partial Content status message mean and how do I fully load resources?).

Check if received more or less than one message

I need to make simple chat application using QTcpSocket and QTcpServer. I understand I should put message size in the beginning of the message to check message bounds. But how should I handle the cases when application receives only part of the message or more than one message?
Or there could be the case when application at first receives incomplete message and after that receives another message. In that situation both messages would be combined in one and only part of second message would be recognized.
You are right. Data from single reading could be incomplete. Read this entry.
You can make up your own protocol for correct data transfer.
Let`s imagine that you have to send text message. You could do it in the next way:
First 4 bytes - message size, next bytes - message.
When you receive first piece of data you can analyze it and understand hove many bytes you are waiting for. When you read all data from the first message, beginning of the next message will contain full message size in the firs 4 bytes.
If your messages have a defined data type (or a series of defined types) and you use Qt >=5.7 then you can use transactions.
The typical example is if your message is a QString (imagine a JSON or XML) you can write it using QDataStream as always, then you read it you can use:
QString messageString;
QDataStream stream(socket);
stream.startTransaction();
stream >> messageString;
if(stream.commitTransaction()){
// The message was received in full
}

What's entity used for in HTTP protocol? [duplicate]

This question already has answers here:
What exactly is an HTTP Entity?
(10 answers)
Closed 9 years ago.
Now I know what's an http entity. But what's entity used for?
I mean, when an application manipulates an http request or response, it just need to know how to parse message head and message body. Then what's the role of an entity? They have almost similar structures.
I dont really understand what you are trying to ask?
If you mean can we skip using HttpEntity in response and request at all? The answer is no!
its a convention you have to follow it, that how internet works!
Quoting entities from apache documentation:
Since an entity can represent both binary and character content, it
has support for character encodings (to support the latter, ie.
character content).
The entity is created when the request was successful, and used to
read the response.
To read the content from the entity, you can either retrieve the input
stream via the HttpEntity.getContent() method, which returns an
InputStream, or you can supply an output stream to the
HttpEntity.writeTo(OutputStream) method, which will return once all
content has been written to the given stream.
When the entity was received as a result of a response, the methods
getContentType() and getContentLength() methods are for reading the
common headers Content-Type and Content-Length respectively (if they
are available). Since the Content-Type header can contain a character
encoding for text mime-types like text/plain or text/html, the
getContentEncoding() method is used to read this information. If the
headers aren't available, a length of -1 will be returned, and NULL
for the content-type. If the Content-Type header is available, a
[Header] object will be returned.
When creating an entity for a request, this meta data has to be
supplied by the creator of the entity.
Other headers from the response are read using the getHeaders()
methods from the response object.
Source: http://wiki.apache.org/HttpComponents/HttpEntity
And I'm again sorry if I didn't get your question right, but hope this helps anyways.

when to use writeUTF() and writeUTFBytes() in ByteArray of AS3

I am trying to create a file format for myself, so i was forming the header for my file. To write a known length string into a ByteArray, which method should i use, writeUTF() or writeUTFBytes().
From the Flex 3 language ref, it tells me that writeUTF() prepends the length of the string and throws a RangeError whereas writeUTFBytes() does not.
Any suggessions would be appreciated.
The only difference between the two is that writeUTFBytes() doesn't prepend the message with the length of the string (The RangeError is because 65535 is the highest number you can store in 16 bits)
Where you'd use one over the other depends on what you're doing. For example, I use writeUTFBytes() when copying a XML object over to be compressed. In this case, I don't care about the length of the string, and it'd introduce something extra to the code.
writeUTF() can be useful if you're writing a streaming/network server, where as you prefix the message length to the message, you know how many bytes to stream on the other end before the end of the message. e.g., I have 200 bytes worth of message. I read the length (16-bit integer), which tells me the message is 100 bytes. I read in 100 bytes and I know it's a complete message. Everything after is another message. If the message length said the message was 300 bytes, then I'd know I'd have to wait a bit before I have the full message.
I think i have found the solution myself. It came to me when i was coding to read back the data. The corresponding functions to read from a bytearray readUTF() and readUTFBytes(length:uint) requires the length to be passed to it.
So if you know the length of the string that you are gonna write, you can use writeUTFBytes() and use readUTFBytes() with that size. Else you can use readUTF(), letting as3 write the size of the data which can be read back without any need to know the length of the string while using readUTF().
Hope this might be useful to some one as well.

getting gmail contacts, can't figure out what to set content length to

I'm trying to retrieve my contacts using curl. I've succeeded in getting my authToken, and now am getting an error stating that I need to set the content-length in the header, but when I set the content length to 0 I get a "bad request" error.
Does anyone know what the content length is? Is it the length of the Auth key? or the length of the entire header field that contains it? I'm just poking around in the dark, and the google api doesn't seem to explain what it's looking for.
According to the HTTP standard, content-length must be greater then or equal to zero. This header can cause a "bad request" problem if:
A 'transfer-encoding' header is included in the request with certain values or
If the content-length is less than the actual length
A content-length less than zero is sent
Content length should be the size of the message body (not including headers). This would include POST data (presumably how your authToken is sent) sent with the request.
The length sent shouldn't need to be exact (though you should try!). Most browsers don't care about the length (as long as it is greater than the actual content length). If it is less than the actual content length, most browsers choke, but not the other way around. I'm assuming Google's servers will act similarly.
So, the solution appears to be that
a) the second request is a GET not a POST
and
b) the username I was passing in requires a fully qualified email (boo#gmail.com, not just boo)

Resources