Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > de.comp.lang.javascript > #5130
| From | Stefan Reuther <stefan.news@arcor.de> |
|---|---|
| Newsgroups | de.comp.lang.javascript |
| Subject | Re: HTTP POST? |
| Date | 2019-06-14 18:08 +0200 |
| Message-ID | <qe0nr3.as.1@stefan.msgid.phost.de> (permalink) |
| References | (7 earlier) <qdl8uu$mkk$1@news2.informatik.uni-stuttgart.de> <qdoplg.4bs.1@stefan.msgid.phost.de> <qdp1ce$ljh$1@news2.informatik.uni-stuttgart.de> <qdrfe7.4t0.1@stefan.msgid.phost.de> <qdrqq8$cu6$1@news2.informatik.uni-stuttgart.de> |
Am 12.06.2019 um 23:28 schrieb Ulli Horlacher:
> Stefan Reuther <stefan.news@arcor.de> wrote:
>> Ich gehe natürlich davon aus, dass ein Übertragungsratenmesstool als
>> Gegenseite einen Übertragensratenmesstoolserver hat und keinen
>> generischen HTTP-Server mit Server-Architektur der 90er. Der Server
>> sollte schon da sein, und nicht erst vom inetd gestartet werden.
>>
>> Einen solchen Server von Null an zu schreiben ist wirklich kein Hexenwerk.
>
> Bitte, mach das, wenn das doch SO einfach ist.
Siehe unten, knappe Dreiviertelstunde. Kann sicher noch ein, zwei
Reviews vertragen, und etwas Härtung gegen DoS/Teergrube, etwas
verbesserte Protokollparserei. Ebenso wäre eine spannende Übung, poll()
zu verwenden und auf fork() zu verzichten. Getestet mit curl und wget.
100k-Download: wget http://127.0.0.1:8000/filename?size=100000
Einen HTTP-Server zu hacken würde ich heute fast als Grundqualifikation
sehen. Das schöne an den RfC-Protokollen ist, dass man mit erstaunlich
wenig Aufwand erstaunlich viel erreichen kann.
>>>> Und all das lässt sich durch Vergrößerung der Datenmenge reduzieren.
>>>> Mehr Daten in der Payload = Kernel hat immer was zu senden = Overhead
>>>> geht besser unter.
>>>
>>> Meine Server machen ca 5 GB/s Durchsatz. In dem Bereich muesste ich Daten
>>> verschicken um den Overhead zu minimieren. Das ist bei langsamer
>>> Client-Anbindung (DSL, WLAN, etc) unbrauchbar.
>>
>> Deswegen adaptiv. Wie lange brauchen 64k? Wenn unter einer Sekunde: wie
>> lange brauchen 256k? Und so weiter.
>
> Auch das ist zu viel Overhead und verfaelscht das Ergebnis.
Irgendwie gewinne ich schon den Eindruck, du willst nur hören/sagen,
dass alles doof ist, aber nur keine Lösung. Adaptiv = solange die
Parameter austarieren, bis der Overhead verschwindet, dann messen.
>>> Einen Proxy kannst du nicht beeinflussen. Du musst den benutzen, so, wie
>>> er ist.
>>
>> Eine synthetische Bandbreitenmessung über einen Proxy ist aber auch
>> ziemlich absurd.
>
> Manche Leute kommen nur ueber einen Proxy ins Internet.
> Die wollen trozdem wissen, wie schnell es geht.
> Mit meinem tcpbm koennen sie das feststellen.
Dann wissen sie, wie schnell HTTP über den Proxy geht. Das wissen sie
aber auch, wenn sie einen Up- oder Download machen. Und dann wissen sie
nicht, wie schnell ein nicht auf HTTP basierendes Protokoll ist. F*EX
war doch sowas?
Stefan
----8<--------8<----[nullserver.c]----8<--------8<----
/*
* Quick & Dirty HTTP server
*
* Accepts any request.
* POST data is just swallowed.
* If the request URI contains a "size=N" token, that is the size of the response in bytes.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#define PORT 8000
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define CHECK(x) check(x, #x)
static int check(int result, const char* what)
{
if (result == -1) {
fprintf(stderr, "error in %s: %s\n", what, strerror(errno));
exit(1);
}
return result;
}
static int read_line(int fd, char* buf, size_t len)
{
char ch;
while (1) {
if (read(fd, &ch, 1) != 1) {
return 0;
}
if (ch == '\n') {
*buf = '\0';
return 1;
}
if (ch != '\r' && len > 1) {
*buf++ = tolower((unsigned char) ch);
--len;
}
}
}
static void handle_connection(int fd)
{
static const char zeroes[16384] = {};
char buffer[16384];
while (1) {
// Read first line
if (!read_line(fd, buffer, sizeof buffer)) {
// Probably means they closed the connection although it was a keepalive connection
return;
}
// Check parameters
int is_post = (strncmp(buffer, "post", 4) == 0);
int keepalive = (strstr(buffer, "http/1.1") != 0);
size_t result_size = 0;
char* p = strstr(buffer, "size=");
if (p) {
result_size = strtoul(p+5, 0, 10);
}
size_t post_size = 0;
// Read header
while (1) {
if (!read_line(fd, buffer, sizeof buffer)) {
printf("Failed to read header line\n");
return;
}
if (buffer[0] == '\0') {
break;
}
if (strncmp(buffer, "content-length:", 15) == 0) {
post_size = strtoul(buffer+15, 0, 0);
}
if (strncmp(buffer, "connection:", 11) == 0) {
keepalive = strstr(buffer, "keep-alive") != 0;
}
}
printf("Request: upload %zd bytes, download %zd bytes\n", post_size, result_size);
// For POST, read body
while (is_post && post_size > 0) {
int n = read(fd, buffer, MIN(post_size, sizeof buffer));
if (n <= 0) {
printf("Failed to read POST body\n");
return;
}
post_size -= n;
}
// Response header
sprintf(buffer,
"HTTP/1.1 200 OK\r\n"
"Connection: %s\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Length: %zd\r\n\r\n",
keepalive ? "keep-alive" : "close",
result_size);
if (write(fd, buffer, strlen(buffer)) != (int)strlen(buffer)) {
printf("Failed to write response header\n");
return;
}
// Send response
while (result_size > 0) {
int n = write(fd, zeroes, MIN(result_size, sizeof(zeroes)));
if (n <= 0) {
printf("Failed to write response\n");
return;
}
result_size -= n;
}
if (!keepalive) {
break;
}
}
}
int main()
{
// Set up a socket
int listen_fd;
struct sockaddr_in me;
CHECK(listen_fd = socket(AF_INET, SOCK_STREAM, 0));
int one = 1;
CHECK(setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)));
me.sin_family = AF_INET;
me.sin_port = htons(PORT);
me.sin_addr.s_addr = 0;
CHECK(bind(listen_fd, (struct sockaddr*)&me, sizeof(me)));
CHECK(listen(listen_fd, 10));
// Main loop
printf("Listening...\n");
while (1) {
struct sockaddr_in them;
socklen_t their_size = sizeof(them);
int connection_fd;
CHECK(connection_fd = accept(listen_fd, (struct sockaddr*)&them, &their_size));
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;
CHECK(setsockopt(connection_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)));
CHECK(setsockopt(connection_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)));
pid_t pid = fork();
if (pid == 0) {
// Child
close(listen_fd);
handle_connection(connection_fd);
close(connection_fd);
_exit(0);
} else {
// Parent
printf("Got connection fd=%d, pid=%d\n", connection_fd, (int) pid);
close(connection_fd);
// Reap ripe children
pid_t other_child;
int status;
while ((other_child = waitpid(-1, &status, WNOHANG)) > 0) {
printf("Child %d done\n", (int) other_child);
}
}
}
}
Back to de.comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar
HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-05-31 18:04 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-05-31 21:03 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 13:18 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-01 14:15 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 14:34 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 14:37 +0200
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-01 15:11 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 15:44 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-01 16:07 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 20:17 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-01 18:43 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 20:19 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-01 22:45 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-02 05:02 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-02 18:05 +0200
Re: HTTP POST? Claus Reibenstein <4spamersonly@kabelmail.de> - 2019-06-02 09:37 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-02 13:39 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-02 18:12 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-02 04:06 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-02 18:22 +0200
Re: HTTP POST? Jan Novak <repcom@gmail.com> - 2019-06-03 14:54 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-02 10:44 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-02 14:00 +0200
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-02 17:14 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-02 21:02 +0000
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-03 00:06 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-03 00:47 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-03 06:06 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-03 18:43 +0200
Re: HTTP POST? Claus Reibenstein <4spamersonly@kabelmail.de> - 2019-06-03 12:38 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-03 15:51 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-03 20:26 +0000
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-04 19:31 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-05 06:09 +0000
Re: HTTP POST? Claus Reibenstein <4spamersonly@kabelmail.de> - 2019-06-04 21:48 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-05 06:14 +0000
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-03 18:34 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-03 20:31 +0000
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-04 18:09 +0200
Re: HTTP POST? Manfred Haertel <Manfred.Haertel@rz-online.de> - 2019-06-05 05:50 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-05 06:30 +0000
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-05 15:07 +0200
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-05 18:09 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-05 20:25 +0000
Re: HTTP POST? Claus Reibenstein <4spamersonly@kabelmail.de> - 2019-06-06 09:49 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-06 08:47 +0000
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-08 14:53 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-08 19:31 +0000
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-08 14:50 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-08 19:34 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-09 00:45 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-10 17:29 +0200
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-09 11:10 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-09 12:06 +0000
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-10 11:16 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-10 09:47 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-10 20:35 +0200
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-11 17:50 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-11 20:02 +0000
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-12 07:45 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-12 06:08 +0000
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-12 10:16 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-12 09:05 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-12 10:37 +0200
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-12 18:14 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-12 21:28 +0000
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-12 23:55 +0200
Re: HTTP POST? Manfred Haertel <Manfred.Haertel@rz-online.de> - 2019-06-13 05:35 +0200
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-13 07:40 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-15 18:33 +0200
Re: HTTP POST? Stefan Reuther <stefan.news@arcor.de> - 2019-06-14 18:08 +0200
Re: HTTP POST? Claus Reibenstein <4spamersonly@kabelmail.de> - 2019-06-10 17:37 +0200
Re: HTTP POST? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2019-06-10 16:31 +0000
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-10 21:45 +0200
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-10 22:04 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-11 00:23 +0200
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-11 00:33 +0200
Re: HTTP POST? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-11 01:01 +0200
Re: HTTP POST? Ralph Aichinger <ra@pi.h5.or.at> - 2019-06-10 20:24 +0200
Re: HTTP POST? Arno Welzel <usenet@arnowelzel.de> - 2019-06-11 10:48 +0200
Re: HTTP POST? Claus Reibenstein <4spamersonly@kabelmail.de> - 2019-06-11 11:57 +0200
csiph-web