Class Message

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public final class Message extends GObject

Represents an HTTP message being sent or received.

A Message represents an HTTP message that is being sent or received.

You would create a Message with Message() or fromUri(String, Uri), set up its fields appropriately, and send it.

Message:status-code will normally be a Status value, eg, Status.OK, though of course it might actually be an unknown status code. Message:reason-phrase is the actual text returned from the server, which may or may not correspond to the "standard" description of statusCode. At any rate, it is almost certainly not localized, and not very descriptive even if it is in the user's language; you should not use Message:reason-phrase in user-visible messages. Rather, you should look at Message:status-code, and determine an end-user-appropriate message based on that and on what you were trying to do.

Note that libsoup's terminology here does not quite match the HTTP specification: in RFC 2616, an "HTTP-message" is either a Request, or a Response. In libsoup, a Message combines both the request and the response.

  • Constructor Details

    • Message

      public Message(MemorySegment address)
      Create a Message instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Message

      public Message(String method, String uriString)
      Creates a new empty Message, which will connect to uri.
      Parameters:
      method - the HTTP method for the created request
      uriString - the destination endpoint (as a string)
    • Message

      public Message()
      Create a new Message.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Message class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Message asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • fromEncodedForm

      public static @Nullable Message fromEncodedForm(String method, String uriString, String encodedForm)

      Creates a new Message and sets it up to send the given encodedForm to uri via method. If method is "GET", it will include the form data into uri's query field, and if method is "POST" or "PUT", it will be set as request body.

      This function takes the ownership of encodedForm, that will be released with GLib#free when no longer in use. See also Soup.formEncode(String, Object...), Soup.formEncodeHash(HashTable) and Soup.formEncodeDatalist(Data[]).

      Parameters:
      method - the HTTP method for the created request (GET, POST or PUT)
      uriString - the destination endpoint (as a string)
      encodedForm - a encoded form
      Returns:
      the new SoupMessage, or null if uriString could not be parsed or method is not "GET, "POST" or "PUT"
    • fromMultipart

      public static @Nullable Message fromMultipart(String uriString, Multipart multipart)
      Creates a new Message and sets it up to send multipart to uriString via POST.
      Parameters:
      uriString - the destination endpoint
      multipart - a SoupMultipart
      Returns:
      the new SoupMessage, or null if uriString could not be parsed
    • fromUri

      public static Message fromUri(String method, Uri uri)
      Creates a new empty Message, which will connect to uri.
      Parameters:
      method - the HTTP method for the created request
      uri - the destination endpoint
      Returns:
      the new SoupMessage
    • optionsPing

      public static Message optionsPing(Uri baseUri)
      Creates a new Message to send OPTIONS * to a server. The path of baseUri will be ignored.
      Parameters:
      baseUri - the destination endpoint
      Returns:
      the new SoupMessage
    • addFlags

      public void addFlags(Set<MessageFlags> flags)
      Adds flags to the set of msg's flags.
      Parameters:
      flags - a set of SoupMessageFlags values
    • addFlags

      public void addFlags(MessageFlags... flags)
      Adds flags to the set of msg's flags.
      Parameters:
      flags - a set of SoupMessageFlags values
    • addHeaderHandler

      public int addHeaderHandler(String signal, String header, @Nullable Callback callback)

      Adds a signal handler to this Message for signal.

      Similar to org.gnome.gobject.GObjects.signalConnect, but the callback will only be run if msg's incoming messages headers (that is, the request_headers) contain a header named header.

      Parameters:
      signal - signal to connect the handler to.
      header - HTTP response header to match against
      callback - the header handler
      Returns:
      the handler ID from org.gnome.gobject.GObjects.signalConnect
    • addStatusCodeHandler

      public int addStatusCodeHandler(String signal, int statusCode, @Nullable Callback callback)

      Adds a signal handler to this Message for signal.

      Similar to org.gnome.gobject.GObjects.signalConnect, but the callback will only be run if this Message has the status statusCode.

      signal must be a signal that will be emitted after msg's status is set (this means it can't be a "wrote" signal).

      Parameters:
      signal - signal to connect the handler to.
      statusCode - status code to match against
      callback - the header handler
      Returns:
      the handler ID from org.gnome.gobject.GObjects.signalConnect
    • disableFeature

      public void disableFeature(Type featureType)

      Disables the actions of SessionFeatures with the given featureType (or a subclass of that type) on msg.

      this Message is processed as though the feature(s) hadn't been added to the session. Eg, passing SOUP_TYPE_CONTENT_SNIFFER for featureType will disable Content-Type sniffing on the message.

      You must call this before queueing this Message on a session; calling it on a message that has already been queued is undefined. In particular, you cannot call this on a message that is being requeued after a redirect or authentication.

      Parameters:
      featureType - the GType of a SessionFeature
    • getConnectionId

      public long getConnectionId()

      Returns the unique idenfier for the last connection used.

      This may be 0 if it was a cached resource or it has not gotten a connection yet.

      Returns:
      An id or 0 if no connection.
    • getFirstParty

      public Uri getFirstParty()
      Gets msg's first-party GLib.Uri.
      Returns:
      the msg's first party GUri
    • getFlags

      public Set<MessageFlags> getFlags()
      Gets the flags on msg.
      Returns:
      the flags
    • getForceHttp1

      public boolean getForceHttp1()
      Returns whether HTTP/1 version is currently demanded for the this Message send.
      Returns:
      true, when HTTP/1 is demanded, false otherwise.
      Since:
      3.4
    • getHttpVersion

      public HTTPVersion getHttpVersion()

      Gets the HTTP version of msg.

      This is the minimum of the version from the request and the version from the response.

      Returns:
      the HTTP version
    • getIsOptionsPing

      public boolean getIsOptionsPing()
      Gets whether this Message is intended to be used to send OPTIONS * to a server.
      Returns:
      true if the message is options ping, or false otherwise
    • getIsTopLevelNavigation

      public boolean getIsTopLevelNavigation()

      Returns if this message is set as a top level navigation.

      Used for same-site policy checks.

      Returns:
      Whether the current request is a top-level navitation
    • getMethod

      public String getMethod()
      Returns the method of this message.
      Returns:
      A method such as SOUP_METHOD_GET
    • getMetrics

      public @Nullable MessageMetrics getMetrics()

      Get the MessageMetrics of msg.

      If the flag MessageFlags.COLLECT_METRICS is not enabled for this Message this will return null.

      Returns:
      a SoupMessageMetrics
    • getPriority

      public MessagePriority getPriority()

      Retrieves the MessagePriority.

      If not set this value defaults to SOUP_MESSAGE_PRIORITY_NORMAL.

      Returns:
      the priority of the message.
    • getReasonPhrase

      public @Nullable String getReasonPhrase()
      Returns the reason phrase for the status of this message.
      Returns:
      the phrase
    • getRemoteAddress

      public @Nullable SocketAddress getRemoteAddress()

      Get the remote SocketAddress of the connection associated with the message.

      The returned address can be null if the connection hasn't been established yet, or the resource was loaded from the disk cache. In case of proxy connections, the remote address returned is a ProxyAddress. If Session:remote-connectable is set the returned address id for the connection to the session's remote connectable.

      Returns:
      a GSocketAddress or null if the connection hasn't been established
    • getRequestHeaders

      public MessageHeaders getRequestHeaders()
      Returns the headers sent with the request.
      Returns:
      The MessageHeaders
    • getResponseHeaders

      public MessageHeaders getResponseHeaders()
      Returns the headers recieved with the response.
      Returns:
      The MessageHeaders
    • getSiteForCookies

      public Uri getSiteForCookies()
      Gets msg's site for cookies GUri.
      Returns:
      the msg's site for cookies GUri
    • getStatus

      public Status getStatus()
      Returns the set status of this message.
      Returns:
      The SoupStatus
    • getTlsCiphersuiteName

      public String getTlsCiphersuiteName()
      Gets the name of the TLS ciphersuite negotiated for msg's connection.
      Returns:
      the name of the TLS ciphersuite, or null if msg's connection is not SSL.
    • getTlsPeerCertificate

      public @Nullable TlsCertificate getTlsPeerCertificate()

      Gets the peer's TlsCertificate associated with msg's connection.

      Note that this is not set yet during the emission of Message::accept-certificate signal.

      Returns:
      msg's TLS peer certificate, or null if msg's connection is not SSL.
    • getTlsPeerCertificateErrors

      public Set<TlsCertificateFlags> getTlsPeerCertificateErrors()
      Gets the errors associated with validating msg's TLS peer certificate. Note that this is not set yet during the emission of Message::accept-certificate signal.
      Returns:
      a GTlsCertificateFlags with msg's TLS peer certificate errors.
    • getTlsProtocolVersion

      public TlsProtocolVersion getTlsProtocolVersion()

      Gets the TLS protocol version negotiated for msg's connection.

      If the message connection is not SSL, TlsProtocolVersion.UNKNOWN is returned.

      Returns:
      a GTlsProtocolVersion
    • getUri

      public Uri getUri()
      Gets msg's URI.
      Returns:
      the URI this Message is targeted for.
    • isFeatureDisabled

      public boolean isFeatureDisabled(Type featureType)

      Get whether SessionFeatures of the given featureType (or a subclass of that type) are disabled on msg.

      See disableFeature(Type).

      Parameters:
      featureType - the GType of a SessionFeature
      Returns:
      true if feature is disabled, or false otherwise.
    • isKeepalive

      public boolean isKeepalive()

      Determines whether or not msg's connection can be kept alive for further requests after processing msg.

      The result is based on the HTTP version, Connection header, etc.

      Returns:
      true or false.
    • queryFlags

      public boolean queryFlags(Set<MessageFlags> flags)
      Queries if flags are present in the set of msg's flags.
      Parameters:
      flags - a set of SoupMessageFlags values
      Returns:
      true if flags are enabled in this Message
    • queryFlags

      public boolean queryFlags(MessageFlags... flags)
      Queries if flags are present in the set of msg's flags.
      Parameters:
      flags - a set of SoupMessageFlags values
      Returns:
      true if flags are enabled in this Message
    • removeFlags

      public void removeFlags(Set<MessageFlags> flags)
      Removes flags from the set of msg's flags.
      Parameters:
      flags - a set of SoupMessageFlags values
    • removeFlags

      public void removeFlags(MessageFlags... flags)
      Removes flags from the set of msg's flags.
      Parameters:
      flags - a set of SoupMessageFlags values
    • setFirstParty

      public void setFirstParty(Uri firstParty)

      Sets firstParty as the main document GUri for msg.

      For details of when and how this is used refer to the documentation for CookieJarAcceptPolicy.

      Parameters:
      firstParty - the GUri for the msg's first party
    • setFlags

      public void setFlags(Set<MessageFlags> flags)
      Sets the specified flags on msg.
      Parameters:
      flags - a set of SoupMessageFlags values
    • setFlags

      public void setFlags(MessageFlags... flags)
      Sets the specified flags on msg.
      Parameters:
      flags - a set of SoupMessageFlags values
    • setForceHttp1

      public void setForceHttp1(boolean value)

      Sets whether HTTP/1 version should be used when sending this message. Some connections can still override it, if needed.

      Note the value is unset after the message send is finished.

      Parameters:
      value - value to set
      Since:
      3.4
    • setIsOptionsPing

      public void setIsOptionsPing(boolean isOptionsPing)

      Set whether this Message is intended to be used to send OPTIONS * to a server.

      When set to true, the path of Message:uri will be ignored and Message:method set to SOUP_METHOD_OPTIONS.

      Parameters:
      isOptionsPing - the value to set
    • setIsTopLevelNavigation

      public void setIsTopLevelNavigation(boolean isTopLevelNavigation)

      Sets whether the current request is a top-level navitation.

      See the same-site spec for more information.

      Parameters:
      isTopLevelNavigation - if true indicate the current request is a top-level navigation
    • setMethod

      public void setMethod(String method)
      Set msg's HTTP method to method.
      Parameters:
      method - the value to set
    • setPriority

      public void setPriority(MessagePriority priority)

      Sets the priority of a message.

      Note that this won't have any effect unless used before the message is added to the session's message processing queue.

      The message will be placed just before any other previously added message with lower priority (messages with the same priority are processed on a FIFO basis).

      Setting priorities does not currently work with synchronous messages because in the synchronous/blocking case, priority ends up being determined semi-randomly by thread scheduling.

      Parameters:
      priority - the SoupMessagePriority
    • setRequestBody

      public void setRequestBody(@Nullable String contentType, @Nullable InputStream stream, long contentLength)

      Set the request body of a Message.

      If contentType is null and stream is not null the Content-Type header will not be changed if present. The request body needs to be set again in case this Message is restarted (in case of redirection or authentication).

      Parameters:
      contentType - MIME Content-Type of the body, or null if unknown
      stream - a GInputStream to read the request body from
      contentLength - the byte length of stream or -1 if unknown
    • setRequestBodyFromBytes

      public void setRequestBodyFromBytes(@Nullable String contentType, byte @Nullable [] bytes)

      Set the request body of a Message from GLib.Bytes.

      If contentType is null and bytes is not null the Content-Type header will not be changed if present. The request body needs to be set again in case this Message is restarted (in case of redirection or authentication).

      Parameters:
      contentType - MIME Content-Type of the body, or null if unknown
      bytes - a GBytes with the request body data
    • setSiteForCookies

      public void setSiteForCookies(@Nullable Uri siteForCookies)

      Sets siteForCookies as the policy URL for same-site cookies for msg.

      It is either the URL of the top-level document or null depending on whether the registrable domain of this document's URL matches the registrable domain of its parent's/opener's URL. For the top-level document it is set to the document's URL.

      See the same-site spec for more information.

      Parameters:
      siteForCookies - the GUri for the msg's site for cookies
    • setTlsClientCertificate

      public void setTlsClientCertificate(@Nullable TlsCertificate certificate)

      Sets the certificate to be used by msg's connection when a client certificate is requested during the TLS handshake.

      You can call this as a response to Message::request-certificate signal, or before the connection is started. If certificate is null the handshake will continue without providing a GTlsCertificate. Note that the TlsCertificate set by this function will be ignored if Session:tls-interaction is not null.

      Parameters:
      certificate - the GTlsCertificate to set, or null
    • setUri

      public void setUri(Uri uri)

      Sets msg's URI to uri.

      If this Message has already been sent and you want to re-send it with the new URI, you need to send it again.

      Parameters:
      uri - the new GUri
    • tlsClientCertificatePasswordRequestComplete

      public void tlsClientCertificatePasswordRequestComplete()

      Completes a certificate password request.

      You must call this as a response to Message::request-certificate-password signal, to notify this Message that the TlsPassword has already been updated.

    • onAcceptCertificate

      Emitted during the msg's connection TLS handshake after an unacceptable TLS certificate has been received.

      You can return true to accept tlsCertificate despite tlsErrors.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitAcceptCertificate

      public boolean emitAcceptCertificate(@Nullable TlsCertificate tlsPeerCertificate, Set<TlsCertificateFlags> tlsPeerErrors)
      Emits the "accept-certificate" signal. See onAcceptCertificate(Message.AcceptCertificateCallback).
    • onAuthenticate

      Emitted when the message requires authentication.

      If credentials are available call Auth.authenticate(String, String) on auth. If these credentials fail, the signal will be emitted again, with retrying set to true, which will continue until you return without calling Auth.authenticate(String, String) on auth.

      Note that this may be emitted before msg's body has been fully read.

      You can authenticate auth asynchronously by calling GObject#ref on auth and returning true. The operation will complete once either Auth.authenticate(String, String) or Auth.cancel() are called.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitAuthenticate

      public boolean emitAuthenticate(@Nullable Auth auth, boolean retrying)
      Emits the "authenticate" signal. See onAuthenticate(Message.AuthenticateCallback).
    • onContentSniffed

      This signal is emitted after Message::got-headers.

      If content sniffing is disabled, or no content sniffing will be performed, due to the sniffer deciding to trust the Content-Type sent by the server, this signal is emitted immediately after Message::got-headers, and type is null.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitContentSniffed

      public void emitContentSniffed(String type, @Nullable HashTable<String,String> params)
      Emits the "content-sniffed" signal. See onContentSniffed(Message.ContentSniffedCallback).
    • onFinished

      Emitted when all HTTP processing is finished for a message.

      (After Message::got_body).

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitFinished

      public void emitFinished()
      Emits the "finished" signal. See onFinished(Message.FinishedCallback).
    • onGotBody

      Emitted after receiving the complete message response body.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitGotBody

      public void emitGotBody()
      Emits the "got-body" signal. See onGotBody(Message.GotBodyCallback).
    • onGotBodyData

      Emitted after reading a portion of the message body from the network.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      3.4
      See Also:
    • emitGotBodyData

      public void emitGotBodyData(int chunkSize)
      Emits the "got-body-data" signal. See onGotBodyData(Message.GotBodyDataCallback).
    • onGotHeaders

      Emitted after receiving the Status-Line and response headers.

      See also addHeaderHandler(String, String, Callback) and addStatusCodeHandler(String, int, Callback), which can be used to connect to a subset of emissions of this signal.

      If you cancel or requeue msg while processing this signal, then the current HTTP I/O will be stopped after this signal emission finished, and msg's connection will be closed. (If you need to requeue a message--eg, after handling authentication or redirection--it is usually better to requeue it from a Message::got-body handler rather than a Message::got_headers handler, so that the existing HTTP connection can be reused.)

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitGotHeaders

      public void emitGotHeaders()
      Emits the "got-headers" signal. See onGotHeaders(Message.GotHeadersCallback).
    • onGotInformational

      Emitted after receiving a 1xx (Informational) response for a (client-side) message.

      The response_headers will be filled in with the headers associated with the informational response; however, those header values will be erased after this signal is done.

      If you cancel or requeue msg while processing this signal, then the current HTTP I/O will be stopped after this signal emission finished, and msg's connection will be closed.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitGotInformational

      public void emitGotInformational()
      Emits the "got-informational" signal. See onGotInformational(Message.GotInformationalCallback).
    • onHstsEnforced

      Emitted when HSTSEnforcer has upgraded the protocol for msg to HTTPS as a result of matching its domain with a HSTS policy.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitHstsEnforced

      public void emitHstsEnforced()
      Emits the "hsts-enforced" signal. See onHstsEnforced(Message.HstsEnforcedCallback).
    • onNetworkEvent

      Emitted to indicate that some network-related event related to msg has occurred.

      This essentially proxies the Gio.SocketClient::event signal, but only for events that occur while msg "owns" the connection; if msg is sent on an existing persistent connection, then this signal will not be emitted. (If you want to force the message to be sent on a new connection, set the MessageFlags.NEW_CONNECTION flag on it.)

      See Gio.SocketClient::event for more information on what the different values of event correspond to, and what connection will be in each case.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitNetworkEvent

      public void emitNetworkEvent(SocketClientEvent event, @Nullable IOStream connection)
      Emits the "network-event" signal. See onNetworkEvent(Message.NetworkEventCallback).
    • onRequestCertificate

      Emitted during the msg's connection TLS handshake when tlsConnection requests a certificate from the client.

      You can set the client certificate by calling setTlsClientCertificate(TlsCertificate) and returning true. It's possible to handle the request asynchornously by returning true and call setTlsClientCertificate(TlsCertificate) later once the certificate is available. Note that this signal is not emitted if Session:tls-interaction was set, or if setTlsClientCertificate(TlsCertificate) was called before the connection TLS handshake started.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitRequestCertificate

      public boolean emitRequestCertificate(@Nullable TlsClientConnection tlsConnection)
      Emits the "request-certificate" signal. See onRequestCertificate(Message.RequestCertificateCallback).
    • onRequestCertificatePassword

      Emitted during the msg's connection TLS handshake when tlsConnection requests a certificate password from the client.

      You can set the certificate password on password, then call tlsClientCertificatePasswordRequestComplete() and return true to handle the signal synchronously. It's possible to handle the request asynchornously by calling GObject#ref on password, then returning true and call tlsClientCertificatePasswordRequestComplete() later after setting the password on password. Note that this signal is not emitted if Session:tls-interaction was set.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitRequestCertificatePassword

      public boolean emitRequestCertificatePassword(@Nullable TlsPassword tlsPassword)
      Emits the "request-certificate-password" signal. See onRequestCertificatePassword(Message.RequestCertificatePasswordCallback).
    • onRestarted

      Emitted when a request that was already sent once is now being sent again.

      e.g. because the first attempt received a redirection response, or because we needed to use authentication.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitRestarted

      public void emitRestarted()
      Emits the "restarted" signal. See onRestarted(Message.RestartedCallback).
    • onStarting

      Emitted just before a message is sent.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitStarting

      public void emitStarting()
      Emits the "starting" signal. See onStarting(Message.StartingCallback).
    • onWroteBody

      Emitted immediately after writing the complete body for a message.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitWroteBody

      public void emitWroteBody()
      Emits the "wrote-body" signal. See onWroteBody(Message.WroteBodyCallback).
    • onWroteBodyData

      Emitted immediately after writing a portion of the message body to the network.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitWroteBodyData

      public void emitWroteBodyData(int chunkSize)
      Emits the "wrote-body-data" signal. See onWroteBodyData(Message.WroteBodyDataCallback).
    • onWroteHeaders

      Emitted immediately after writing the request headers for a message.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitWroteHeaders

      public void emitWroteHeaders()
      Emits the "wrote-headers" signal. See onWroteHeaders(Message.WroteHeadersCallback).
    • builder

      public static Message.Builder<? extends Message.Builder> builder()
      A Message.Builder object constructs a Message with the specified properties. Use the various set...() methods to set properties, and finish construction with Message.Builder.build().
      Returns:
      the builder object