-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gemini.c
56 lines (48 loc) · 1.42 KB
/
Gemini.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Gemini.h"
#include "URL.h"
STREAM *GeminiOpen(const char *URL, const char *Config)
{
STREAM *S;
char *Tempstr=NULL, *Token=NULL;
char *Proto=NULL, *Host=NULL, *PortStr=NULL;
const char *ptr;
ParseURL(URL, &Proto, &Host, &PortStr, NULL, NULL, NULL, NULL);
if (! StrValid(PortStr)) PortStr=CopyStr(PortStr, "1965");
Tempstr=MCopyStr(Tempstr, "tls://", Host, ":", PortStr, NULL);
S=STREAMCreate();
if (STREAMConnect(S, Tempstr, Config))
{
Tempstr=MCopyStr(Tempstr, URL, "\r\n", NULL);
STREAMWriteLine(Tempstr, S);
STREAMFlush(S);
Tempstr=STREAMReadLine(Tempstr, S);
ptr=GetToken(Tempstr, "\\S", &Token, 0);
if (StrValid(Token))
{
STREAMSetValue(S, "gemini:StatusCode", Token);
switch(*Token)
{
case '1':
STREAMSetValue(S, "gemini:Query", ptr);
break;
case '2':
STREAMSetValue(S, "gemini:ContentType", ptr);
break;
case '3':
Token=ResolveURL(Token, URL, ptr);
if (! STREAMConnect(S, Token, Config))
{
STREAMClose(S);
S=NULL;
}
break;
}
}
}
Destroy(Tempstr);
Destroy(Token);
Destroy(PortStr);
Destroy(Proto);
Destroy(Host);
return(S);
}