Ανάπτυξη διαδικτυακών εφαρμογών

Διομήδης Σπινέλλης
Τμήμα Διοικητικής Επιστήμης και Τεχνολογίας
Οικονομικό Πανεπιστήμιο Αθηνών
dds@aueb.gr

Περιεχόμενα

Το μοντέλο αναφοράς OSI

Φυσικό επίπεδο

Επίπεδο σύνδεσης

Επίπεδο δικτύου

Πρωτόκολλα διαδικτύου: IPv4, IPv6, IPsec, ICMP

IPv4

    0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Version|  IHL  |Type of Service|          Total Length         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |         Identification        |Flags|      Fragment Offset    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Time to Live |    Protocol   |         Header Checksum       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                       Source Address                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Destination Address                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Επίπεδο μεταφοράς

Πρωτόκολλα διαδικτύου: TCP, UDP, …

TCP

    0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Source Port          |       Destination Port        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                        Sequence Number                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Acknowledgment Number                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Data |           |U|A|P|R|S|F|                               |
   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
   |       |           |G|K|H|T|N|N|                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           Checksum            |         Urgent Pointer        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             data                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Επίπεδο συνόδου

Πρωτόκολλα διαδικτύου: REST, Sockets API

Επίπεδο παρουσίασης

(Σε παρένθεση σχετικά πρωτόκολλα του διαδικτύου)

Επίπεδο εφαρμογής

(Σε παρένθεση σχετικά πρωτόκολλα του διαδικτύου)

Αρχιτεκτονική του παγκόσμιου ιστού

Προσδιορισμός στοιχείων με URI

URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]
index.html
https://www.dmst.aueb.gr/dds/
https://www.google.com/search?q=AUEB
https://www.aueb.gr/Hmerologio.pdf#page=3

Το πρωτόκολλο HTTP

Το πρωτόκολλο HTTP υποστηρίζει τις παρακάτω μεθόδους επικοινωνίας:

Παράδειγμα HTTP

Αίτηση

GET /dds/ HTTP/1.1
Host: www2.dmst.aueb.gr
User-Agent: curl/7.71.1
Accept: */*

Απάντηση

HTTP/1.1 200 OK
Date: Sun, 08 Jan 2023 10:00:32 GMT
Server: Apache/1.3.33 (Linux/RHEL)
Content-Location: index.en.html
Vary: negotiate,accept-language,accept-charset
TCN: choice
Last-Modified: Fri, 30 Dec 2022 21:03:52 GMT
Accept-Ranges: bytes
Content-Length: 22599
Content-Type: text/html
Content-Language: en

Αίτημα HTTP στη Java


import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class UrlRetriever {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: UrlRetriever URL");
            System.exit(1);
        }

        try {
            // Make an HTTP or HTTPS request with bounded connection and request times.
            var request = HttpRequest.newBuilder(URI.create(args[0]))
                .timeout(Duration.ofSeconds(30))
                .GET()
                .build();
            var client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofSeconds(10))
                .followRedirects(HttpClient.Redirect.NORMAL)
                .build();

            // Decode the response using its declared charset, defaulting to UTF-8.
            var response = client.send(request, HttpResponse.BodyHandlers.ofString());
            if (response.statusCode() >= 400) {
                System.err.println("HTTP error: " + response.statusCode());
                System.exit(1);
            }
            System.out.println(response.body());
        } catch (IllegalArgumentException e) {
            System.err.println("Invalid HTTP(S) URL: " + e.getMessage());
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Request failed: " + e.getMessage());
            System.exit(1);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Request interrupted");
            System.exit(1);
        }
    }
}

Χαρακτηριστικά της αρχιτεκτονικής REST

Παραδείγματα αιτήσεων REST


java UrlRetriever https://www.gutenberg.org/cache/epub/1342/pg1342.txt

curl 'https://aviationweather.gov/api/data/metar?ids=LGAV&hours=48'
curl -v -k "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&titles=Moon&sites=enwiki" |  jq .
curl -A "Mozilla/5.0" 'http://telematics.oasa.gr/api/?act=getBusLocation&p1=1822'
curl -LH "Accept: application/vnd.crossref.unixref+xml;q=1, application/rdf+json;q=0.5" https://doi.org/10.1126/science.169.3946.635

curl --request POST -H "Authorization: Bearer $ROBBIE_API_KEY" --url http://robbie.dmst.aueb.gr:50135/completion --header "Content-Type: application/json" --data '{ "prompt": "You are a helpful Assistant responding to User queries. User: Which city is the capital of Germany?\nAssistant: Berlin\nWhich city is the capital of Greece?"}'

curl https://api.openai.com/v1/engines/davinci/completions \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "prompt": "I can convert a String into an int in Java program by ",
    "max_tokens": 50
  }'

Εξυπηρετητής ώρας HTTP στη Java


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class TimeServer {
    private static final DateTimeFormatter DATE_FORMAT =
        DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ROOT);

    static class GetHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            try {
                var method = exchange.getRequestMethod();
                if (!method.equals("GET") && !method.equals("HEAD")) {
                    exchange.getResponseHeaders().set("Allow", "GET, HEAD");
                    exchange.sendResponseHeaders(405, -1);
                    return;
                }

                byte[] response = (LocalDateTime.now().format(DATE_FORMAT) + "\n")
                    .getBytes(StandardCharsets.UTF_8);

                // Describe the text encoding and send the response.
                exchange.getResponseHeaders().set("Content-Type", "text/plain; charset=UTF-8");
                if (method.equals("HEAD")) {
                    exchange.getResponseHeaders().set("Content-Length", Integer.toString(response.length));
                    exchange.sendResponseHeaders(200, -1);
                } else {
                    exchange.sendResponseHeaders(200, response.length);
                    exchange.getResponseBody().write(response);
                }
                System.out.println(exchange.getRemoteAddress());
            } finally {
                exchange.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // Create a server on port 8000
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);

        server.createContext("/", new GetHandler()); // "GET /" handler

        server.start(); // Start serving requests
    }
}

Διαδικτυακές εφαρμογές στην πράξη

Άσκηση: Επικοινωνία Πελάτη-Εξυπηρετητή

Άσκηση 18

Μπορείτε να κατεβάσετε το αντίστοιχο αρχείο και να στείλετε τους βαθμούς σας από τους δεσμούς που βρίσκονται στη σελίδα των ασκήσεων.

Βιβλιογραφία

  • Rogers Candenhead. Πλήρες Εγχειρίδιο της Java 12. 8η Έκδοση. Εκδόσεις X. Γκιούρδα, 2023. Κεφ. 17, 20.
  • Περιεχόμενα