Frage

Hi I have a simple restlet get method which returns a static string. It looks like the following:

@Get
    public String represent() {
        return "mystring\r\n";
    }

A low level c app is invoking this get by going into a read loop. It never receives a finish confirmation signaling it that there is no more data left to read and times out after 20 seconds. Is there code I need to send to alert the client app that no more data is coming? Or that the get is finished?

War es hilfreich?

Lösung

[Note: The code written below is partly based on the samples available on http://www.restlet.org ]

HTTP 1.0 and 1.1 have a header named Content-Length. Whatever is the numeric value of that header, is the length of the body of the HTTP-response. Going a step further in HTTP 1.1, there is another header name-value, Tranfer-Encoding: chunked which indicates that the response-body is divided into parts (chunks) and each part's length is mentioned in a line just before that part is delivered.(I am not including other values of Transfer-Encoding to keep this answer concised.)

If this is my restlet server:

package restletapp;

import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

public class RestletApp extends ServerResource {

    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);
        component.getDefaultHost().attach("/trace", RestletApp.class);
        component.start();
    }

    @Get
    public String toAtGet() {
        return  "Resource URI  : " + getReference() + '\n'
              + "Root URI      : " + getRootRef() + '\n'
              + "Routed part   : " + getReference().getBaseRef() + '\n'
              + "Remaining part: " + getReference().getRemainingPart()
                ;
    }

}

And this is my client (written using Sockets in Java. Just sends a minimal HTTP request, and prints each character of response on console.)

package restletapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Requester {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s=new Socket("localhost", 8182);
        OutputStream os = s.getOutputStream();
        os.write((
                  "GET /trace HTTP/1.1\r\n" //request
                + "host: localhost:8182\r\n" //request
                + "Connection-type: close\r\n\r\n" //request
                ).getBytes());
        InputStream is = s.getInputStream();
        for(int ch;(ch=is.read())!=-1;System.out.flush())
            System.out.write(ch); //response, one char at a time.
        is.close();
        os.close();
        s.close();
    }
}

The client process never ends. But if I change my client program to this:

package restletapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Requester {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s=new Socket("localhost", 8182);
        OutputStream os = s.getOutputStream();
        os.write((
                  "GET /trace HTTP/1.1\r\n"
                + "host: localhost:8182\r\n"
                + "Connection-type: close\r\n\r\n"
                ).getBytes());
        InputStream is = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        int bytesRead=0;
        int contentLength=0;
        //response headers.
        for(String line;!(line=br.readLine()).isEmpty();System.out.flush()){
            System.out.println(line);
            String[] tokens = line.split(":| ");
            if(tokens[0].equalsIgnoreCase("content-length")){
                contentLength=Integer.parseInt(tokens[2]);
            }
        }
        //response separator, between headers and body.
        System.out.println();
        //response body.
        while(bytesRead<contentLength){
            System.out.write(br.read());
            System.out.flush();
            bytesRead++;
        }
        is.close();
        os.close();
        s.close();
    }
}

In the second version of Requester, you can see that the connection is closed by the client when response body's content-length-number of characters are read.

This is what i get using curl:

command line $ curl -i "http://localhost:8182/trace"
HTTP/1.1 200 OK
Date: Fri, 14 Jun 2013 11:54:32 GMT
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 148
Content-Type: text/plain; charset=UTF-8

Resource URI  : http://localhost:8182/trace
Root URI      : http://localhost:8182/trace
Routed part   : http://localhost:8182/trace
Remaining part:
command line $ 

You can see, that curl exits after reading the content.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top