Class MessageHeaders
-
Constructor Summary
ConstructorDescriptionMessageHeaders
(MemorySegment address) Create a MessageHeaders proxy instance for the provided memory address.Creates aSoupMessageHeaders
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Appends a new header with namename
and valuevalue
to this MessageHeaders.void
Removes all the headers listed in the Connection header.void
clear()
Clears this MessageHeaders.void
Callsfunc
once for each header value in this MessageHeaders.void
freeRanges
(Range ranges) Frees the array of ranges returned fromgetRanges(long, io.github.jwharm.javagi.base.Out<org.gnome.soup.Range[]>)
.boolean
Looks up the "Content-Disposition" header in this MessageHeaders, parses it, and returns its value in *disposition
and *params
.long
Gets the message body length that this MessageHeaders declare.boolean
Parses this MessageHeaders's Content-Range header and returns it instart
,end
, andtotalLength
.getContentType
(@Nullable Out<HashTable<String, String>> params) Looks up the "Content-Type" header in this MessageHeaders, parses it, and returns its value in *contentType
and *params
.Gets the message body encoding that this MessageHeaders declare.Gets the expectations declared by this MessageHeaders's "Expect" header.Gets the type of headers.Gets the value of headername
in this MessageHeaders.Gets the value of headername
in this MessageHeaders.boolean
Parses this MessageHeaders's Range header and returns an array of the requested byte ranges.static Type
getType()
Get the GType of the MessageHeaders classboolean
headerContains
(String name, String token) Checks whether the list-valued headername
is present in this MessageHeaders, and contains a case-insensitive match fortoken
.boolean
headerEquals
(String name, String value) Checks whether the headername
is present in this MessageHeaders and is (case-insensitively) equal tovalue
.ref()
Atomically increments the reference count of this MessageHeaders by one.void
Removesname
from this MessageHeaders.void
Replaces the value of the headername
in this MessageHeaders withvalue
.void
setContentDisposition
(String disposition, @Nullable HashTable<String, String> params) Sets the "Content-Disposition" header in this MessageHeaders todisposition
, optionally with additional parameters specified inparams
.void
setContentLength
(long contentLength) Sets the message body length that this MessageHeaders will declare, and sets this MessageHeaders's encoding toEncoding.CONTENT_LENGTH
.void
setContentRange
(long start, long end, long totalLength) Sets this MessageHeaders's Content-Range header according to the given values.void
setContentType
(String contentType, @Nullable HashTable<String, String> params) Sets the "Content-Type" header in this MessageHeaders tocontentType
.void
setEncoding
(Encoding encoding) Sets the message body encoding that this MessageHeaders will declare.void
setExpectations
(Set<Expectation> expectations) Sets this MessageHeaders's "Expect" header according toexpectations
.void
setExpectations
(Expectation... expectations) Sets this MessageHeaders's "Expect" header according toexpectations
.void
setRange
(long start, long end) Sets this MessageHeaders's Range header to request the indicated range.void
Sets this MessageHeaders's Range header to request the indicated ranges.void
unref()
Atomically decrements the reference count of this MessageHeaders by one.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
MessageHeaders
Create a MessageHeaders proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
MessageHeaders
Creates aSoupMessageHeaders
.(
Message
does this automatically for its own headers. You would only need to use this method if you are manually parsing or generating message headers.)- Parameters:
type
- the type of headers- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
-
Method Details
-
getType
-
append
Appends a new header with namename
and valuevalue
to this MessageHeaders.(If there is an existing header with name
name
, then this creates a second one, which is only allowed for list-valued headers; see alsoreplace(java.lang.String, java.lang.String)
.)The caller is expected to make sure that
name
andvalue
are syntactically correct.- Parameters:
name
- the header name to addvalue
- the new value ofname
-
cleanConnectionHeaders
public void cleanConnectionHeaders()Removes all the headers listed in the Connection header. -
clear
public void clear()Clears this MessageHeaders. -
foreach
Callsfunc
once for each header value in this MessageHeaders.Beware that unlike
getList(java.lang.String)
, this processes the headers in exactly the way they were added, rather than concatenating multiple same-named headers into a single value. (This is intentional; it ensures that if you callappend(java.lang.String, java.lang.String)
multiple times with the same name, then the I/O code will output multiple copies of the header when sending the message to the remote implementation, which may be required for interoperability in some cases.)You may not modify the headers from
func
.- Parameters:
func
- callback function to run for each header
-
freeRanges
Frees the array of ranges returned fromgetRanges(long, io.github.jwharm.javagi.base.Out<org.gnome.soup.Range[]>)
.- Parameters:
ranges
- an array ofSoupRange
-
getContentDisposition
Looks up the "Content-Disposition" header in this MessageHeaders, parses it, and returns its value in *disposition
and *params
.params
can benull
if you are only interested in the disposition-type.In HTTP, the most common use of this header is to set a disposition-type of "attachment", to suggest to the browser that a response should be saved to disk rather than displayed in the browser. If
params
contains a "filename" parameter, this is a suggestion of a filename to use. (If the parameter value in the header contains an absolute or relative path, libsoup will truncate it down to just the final path component, so you do not need to test this yourself.)Content-Disposition is also used in "multipart/form-data", however this is handled automatically by
Multipart
and the associated form methods.- Parameters:
disposition
- return location for the disposition-type, ornull
params
- return location for the Content-Disposition parameters, ornull
- Returns:
true
if this MessageHeaders contains a "Content-Disposition" header,false
if not (in which case *disposition
and *params
will be unchanged).
-
getContentLength
public long getContentLength()Gets the message body length that this MessageHeaders declare.This will only be non-0 if
getEncoding()
returnsEncoding.CONTENT_LENGTH
.- Returns:
- the message body length declared by this MessageHeaders.
-
getContentRange
public boolean getContentRange(Out<Long> start, Out<Long> end, @Nullable @Nullable Out<Long> totalLength) Parses this MessageHeaders's Content-Range header and returns it instart
,end
, andtotalLength
. If the total length field in the header was specified as "*", thentotalLength
will be set to -1.- Parameters:
start
- return value for the start of the rangeend
- return value for the end of the rangetotalLength
- return value for the total length of the resource, ornull
if you don't care.- Returns:
true
if this MessageHeaders contained a "Content-Range" header containing a byte range which could be parsed,false
otherwise.
-
getContentType
Looks up the "Content-Type" header in this MessageHeaders, parses it, and returns its value in *contentType
and *params
.params
can benull
if you are only interested in the content type itself.- Parameters:
params
- return location for the Content-Type parameters (eg, "charset"), ornull
- Returns:
- a string with the value of the
"Content-Type" header or
null
if this MessageHeaders does not contain that header or it cannot be parsed (in which case *params
will be unchanged).
-
getEncoding
Gets the message body encoding that this MessageHeaders declare.This may not always correspond to the encoding used on the wire; eg, a HEAD response may declare a Content-Length or Transfer-Encoding, but it will never actually include a body.
- Returns:
- the encoding declared by this MessageHeaders.
-
getExpectations
Gets the expectations declared by this MessageHeaders's "Expect" header.Currently this will either be
Expectation.CONTINUE
orExpectation.UNRECOGNIZED
.- Returns:
- the contents of this MessageHeaders's "Expect" header
-
getHeadersType
-
getList
Gets the value of headername
in this MessageHeaders.Use this for headers whose values are comma-delimited lists, and which are therefore allowed to appear multiple times in the headers. For non-list-valued headers, use
getOne(java.lang.String)
.If
name
appears multiple times in this MessageHeaders,getList(java.lang.String)
will concatenate all of the values together, separated by commas. This is sometimes awkward to parse (eg, WWW-Authenticate, Set-Cookie), but you have to be able to deal with it anyway, because the HTTP spec explicitly states that this transformation is allowed, and so an upstream proxy could do the same thing.- Parameters:
name
- header name- Returns:
- the header's value or
null
if not found.
-
getOne
Gets the value of headername
in this MessageHeaders.Use this for headers whose values are not comma-delimited lists, and which therefore can only appear at most once in the headers. For list-valued headers, use
getList(java.lang.String)
.If this MessageHeaders does erroneously contain multiple copies of the header, it is not defined which one will be returned. (Ideally, it will return whichever one makes libsoup most compatible with other HTTP implementations.)
- Parameters:
name
- header name- Returns:
- the header's value or
null
if not found.
-
getRanges
Parses this MessageHeaders's Range header and returns an array of the requested byte ranges.The returned array must be freed with
freeRanges(org.gnome.soup.Range)
.If
totalLength
is non-0, its value will be used to adjust the returned ranges to have explicit start and end values, and the returned ranges will be sorted and non-overlapping. IftotalLength
is 0, then some ranges may have an end value of -1, as described underRange
, and some of the ranges may be redundant.Beware that even if given a
totalLength
, this function does not check that the ranges are satisfiable.SoupServer
has built-in handling for range requests. If your server handler returns aStatus.OK
response containing the complete response body (rather than pausing the message and returning some of the response body later), and there is a Range header in the request, then libsoup will automatically convert the response to aStatus.PARTIAL_CONTENT
response containing only the range(s) requested by the client.The only time you need to process the Range header yourself is if either you need to stream the response body rather than returning it all at once, or you do not already have the complete response body available, and only want to generate the parts that were actually requested by the client.
- Parameters:
totalLength
- the total_length of the response bodyranges
- return location for an array ofSoupRange
- Returns:
true
if this MessageHeaders contained a syntactically-valid "Range" header,false
otherwise (in which caserange
andlength
will not be set).
-
headerContains
Checks whether the list-valued headername
is present in this MessageHeaders, and contains a case-insensitive match fortoken
.(If
name
is present in this MessageHeaders, then this is equivalent to callingSoup.headerContains(java.lang.String, java.lang.String)
on its value.)- Parameters:
name
- header nametoken
- token to look for- Returns:
true
if the header is present and containstoken
,false
otherwise.
-
headerEquals
-
ref
Atomically increments the reference count of this MessageHeaders by one.- Returns:
- the passed in
SoupMessageHeaders
-
remove
Removesname
from this MessageHeaders.If there are multiple values for
name
, they are all removed.- Parameters:
name
- the header name to remove
-
replace
Replaces the value of the headername
in this MessageHeaders withvalue
.See also
append(java.lang.String, java.lang.String)
.The caller is expected to make sure that
name
andvalue
are syntactically correct.- Parameters:
name
- the header name to replacevalue
- the new value ofname
-
setContentDisposition
public void setContentDisposition(String disposition, @Nullable @Nullable HashTable<String, String> params) Sets the "Content-Disposition" header in this MessageHeaders todisposition
, optionally with additional parameters specified inparams
.See
getContentDisposition(io.github.jwharm.javagi.base.Out<java.lang.String>, io.github.jwharm.javagi.base.Out<org.gnome.glib.HashTable<java.lang.String, java.lang.String>>)
for a discussion of how Content-Disposition is used in HTTP.- Parameters:
disposition
- the disposition-typeparams
- additional parameters
-
setContentLength
public void setContentLength(long contentLength) Sets the message body length that this MessageHeaders will declare, and sets this MessageHeaders's encoding toEncoding.CONTENT_LENGTH
.You do not normally need to call this; if this MessageHeaders is set to use Content-Length encoding, libsoup will automatically set its Content-Length header for you immediately before sending the headers. One situation in which this method is useful is when generating the response to a HEAD request; Calling
setContentLength(long)
allows you to put the correct content length into the response without needing to waste memory by filling in a response body which won't actually be sent.- Parameters:
contentLength
- the message body length
-
setContentRange
public void setContentRange(long start, long end, long totalLength) Sets this MessageHeaders's Content-Range header according to the given values.(Note that
totalLength
is the total length of the entire resource that this is a range of, not simplyend
-start
+ 1.)Server
has built-in handling for range requests, and you do not normally need to call this function youself. SeegetRanges(long, io.github.jwharm.javagi.base.Out<org.gnome.soup.Range[]>)
for more details.- Parameters:
start
- the start of the rangeend
- the end of the rangetotalLength
- the total length of the resource, or -1 if unknown
-
setContentType
-
setEncoding
Sets the message body encoding that this MessageHeaders will declare.In particular, you should use this if you are going to send a request or response in chunked encoding.
- Parameters:
encoding
- aSoupEncoding
-
setExpectations
Sets this MessageHeaders's "Expect" header according toexpectations
.Currently
Expectation.CONTINUE
is the only known expectation value. You should set this value on a request if you are sending a large message body (eg, via POST or PUT), and want to give the server a chance to reject the request after seeing just the headers (eg, because it will require authentication before allowing you to post, or because you're POSTing to a URL that doesn't exist). This saves you from having to transmit the large request body when the server is just going to ignore it anyway.- Parameters:
expectations
- the expectations to set
-
setExpectations
Sets this MessageHeaders's "Expect" header according toexpectations
.Currently
Expectation.CONTINUE
is the only known expectation value. You should set this value on a request if you are sending a large message body (eg, via POST or PUT), and want to give the server a chance to reject the request after seeing just the headers (eg, because it will require authentication before allowing you to post, or because you're POSTing to a URL that doesn't exist). This saves you from having to transmit the large request body when the server is just going to ignore it anyway.- Parameters:
expectations
- the expectations to set
-
setRange
public void setRange(long start, long end) Sets this MessageHeaders's Range header to request the indicated range.start
andend
are interpreted as in aRange
.If you need to request multiple ranges, use
setRanges(org.gnome.soup.Range, int)
.- Parameters:
start
- the start of the range to requestend
- the end of the range to request
-
setRanges
Sets this MessageHeaders's Range header to request the indicated ranges.If you only want to request a single range, you can use
setRange(long, long)
.- Parameters:
ranges
- an array ofSoupRange
length
- the length ofrange
-
unref
public void unref()Atomically decrements the reference count of this MessageHeaders by one.When the reference count reaches zero, the resources allocated by this MessageHeaders are freed
-