Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
unix
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
tu-wien
unix
Commits
72fb0784
Commit
72fb0784
authored
Nov 27, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor the client
parent
712e3910
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
29 deletions
+40
-29
http/client.c
http/client.c
+40
-29
No files found.
http/client.c
View file @
72fb0784
...
...
@@ -36,7 +36,9 @@ int main(int argc, char *argv[]) {
char
*
port
=
"80"
;
char
*
ofile
=
NULL
;
int
opt_f
=
0
;
char
*
dir
=
NULL
;
int
opt_d
=
0
;
char
*
url
=
NULL
;
int
c
;
...
...
@@ -48,44 +50,61 @@ int main(int argc, char *argv[]) {
break
;
case
'o'
:
ofile
=
optarg
;
opt_f
=
1
;
break
;
case
'd'
:
dir
=
optarg
;
opt_d
=
1
;
break
;
case
'h'
:
printf
(
"Command usage:
\n
"
);
print_usage
();
exit
(
EXIT_SUCCESS
)
;
break
;
case
'?'
:
print_usage
();
exit
(
EXIT_FAILURE
)
;
break
;
default:
abort
();
}
}
/// Check for the case that both output file and directory are set
if
(
o
file
!=
NULL
&&
dir
!=
NULL
)
{
printf
(
"Either output file or directory
is
specified. You cannot use both.
\n
"
);
if
(
o
pt_d
==
1
&&
opt_f
==
1
)
{
printf
(
"Either output file or directory
should be
specified. You cannot use both.
\n
"
);
print_usage
();
exit
(
EXIT_FAILURE
);
}
/// Check if the required argument is there
if
(
argv
[
optind
]
==
NULL
)
{
fprintf
(
stderr
,
"Mandatory argument 'url' missing.
\n
"
);
print_usage
();
exit
(
EXIT_FAILURE
);
}
else
{
url
=
argv
[
optind
];
}
char
proto
[
8
];
memcpy
(
proto
,
url
,
7
);
proto
[
7
]
=
0
;
if
(
strcmp
(
proto
,
"http://"
)
!=
0
){
printf
(
"ERROR: The url doesn't start with http://
\n
"
);
print_usage
();
}
char
*
base_url
=
strstr
(
url
,
"http://"
)
+
7
;
//< the base url starts after http://
char
*
path
=
malloc
(
strlen
(
base_url
)
+
1
);
strcpy
(
path
,
base_url
);
//< Copying so that strtok() doesn't change it
path
=
strstr
(
path
,
"/"
);
if
(
path
==
NULL
)
path
=
"/"
;
//< Set the root path if there is none
if
(
path
==
NULL
)
{
path
=
"/"
;
//< Set the root path if there is none
if
(
ofile
==
NULL
&&
dir
!=
NULL
)
ofile
=
"index.html"
;
}
else
{
if
(
ofile
==
NULL
&&
dir
!=
NULL
)
{
ofile
=
strchr
(
path
,
'/'
);
}
}
const
char
*
hostname
=
strtok
(
base_url
,
";/?:@=&"
);
//< Get the FQDN of the remote
struct
addrinfo
*
config
=
get_host_info
(
hostname
,
port
);
//< Check if the address is resolvable
...
...
@@ -120,7 +139,9 @@ int main(int argc, char *argv[]) {
}
static
void
print_usage
(
void
)
{
printf
(
"client [-p PORT] [ -o FILE | -d DIR ] URL
\n
"
);
puts
(
"Usage:"
);
puts
(
"client [-p PORT] [ -o FILE | -d DIR ] URL
\n
"
);
exit
(
EXIT_FAILURE
);
}
/**
...
...
@@ -224,43 +245,33 @@ static void process_response(char reply[sizeof(int32_t)], char *ofile, char *dir
char
*
body
=
strstr
(
reply
,
"
\r\n\r\n
"
)
+
4
;
if
(
ofile
==
NULL
&&
dir
==
NULL
)
printf
(
"%s"
,
body
);
//< Print the body of the response
else
{
if
(
ofile
!=
NULL
)
{
FILE
*
out
=
NULL
;
if
(
ofile
!=
NULL
&&
dir
==
NULL
)
{
out
=
fopen
(
ofile
,
"w"
);
if
(
out
==
NULL
)
{
(
void
)
fprintf
(
stderr
,
"ERROR: Could not open ./%s for writing."
,
ofile
);
exit
(
EXIT_FAILURE
);
}
else
{
f
printf
(
out
,
"%s"
,
body
);
f
write
(
body
,
1
,
strlen
(
body
),
out
);
fclose
(
out
);
printf
(
"File ./%s successfully written.
\n
"
,
ofile
);
}
}
else
if
(
dir
!=
NULL
)
{
FILE
*
out
=
NULL
;
}
else
if
(
ofile
!=
NULL
&&
dir
!=
NULL
)
{
char
path
[
sizeof
(
dir
)
+
100
];
if
(
dir
[
strlen
(
dir
)
-
1
]
==
'/'
)
snprintf
(
path
,
sizeof
path
,
"%sresult.html"
,
dir
);
else
snprintf
(
path
,
sizeof
path
,
"%s/result.html"
,
dir
);
out
=
fopen
(
path
,
"w"
);
//< Open <path>/result.html for writing
snprintf
(
path
,
sizeof
path
,
"%s%s"
,
dir
,
ofile
);
out
=
fopen
(
path
,
"w"
);
//< Open for writing
if
(
out
==
NULL
)
{
if
(
dir
[
strlen
(
dir
)
-
1
]
==
'/'
)
(
void
)
fprintf
(
stderr
,
"ERROR: Could not open %sresult.html for writing.
\n
"
,
dir
);
else
(
void
)
fprintf
(
stderr
,
"ERROR: Could not open %s/result.html for writing.
\n
"
,
dir
);
(
void
)
fprintf
(
stderr
,
"ERROR: Could not open %s%s for writing.
\n
"
,
dir
,
ofile
);
exit
(
EXIT_FAILURE
);
}
else
{
f
printf
(
out
,
"%s"
,
body
);
f
write
(
body
,
1
,
strlen
(
body
),
out
);
fclose
(
out
);
if
(
dir
[
strlen
(
dir
)
-
1
]
==
'/'
)
printf
(
"File %sresult.html successfully written.
\n
"
,
dir
);
else
printf
(
"File %s/result.html successfully written.
\n
"
,
dir
);
printf
(
"File %s%s successfully written.
\n
"
,
dir
,
ofile
);
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment