1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#include "swissknife_lease_json.h" |
6 |
|
|
|
7 |
|
|
#include "json.h" |
8 |
|
|
#include "json_document.h" |
9 |
|
|
#include "util/pointer.h" |
10 |
|
|
|
11 |
|
|
#include "logging.h" |
12 |
|
|
|
13 |
|
6 |
LeaseReply ParseAcquireReply(const CurlBuffer &buffer, |
14 |
|
|
std::string *session_token) { |
15 |
✓✓✗✓ ✓✓ |
6 |
if (buffer.data.size() == 0 || session_token == NULL) { |
16 |
|
2 |
return kLeaseReplyFailure; |
17 |
|
|
} |
18 |
|
|
|
19 |
|
4 |
const UniquePtr<JsonDocument> reply(JsonDocument::Create(buffer.data)); |
20 |
✓✓✗✓ ✓✓ |
4 |
if (!reply || !reply->IsValid()) { |
21 |
|
1 |
return kLeaseReplyFailure; |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
const JSON *result = |
25 |
|
3 |
JsonDocument::SearchInObject(reply->root(), "status", JSON_STRING); |
26 |
✓✗ |
3 |
if (result != NULL) { |
27 |
|
3 |
const std::string status = result->string_value; |
28 |
✓✓ |
3 |
if (status == "ok") { |
29 |
|
1 |
LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
30 |
|
|
const JSON *token = JsonDocument::SearchInObject( |
31 |
|
1 |
reply->root(), "session_token", JSON_STRING); |
32 |
✓✗ |
1 |
if (token != NULL) { |
33 |
|
|
LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", |
34 |
|
1 |
token->string_value); |
35 |
|
1 |
*session_token = token->string_value; |
36 |
|
1 |
return kLeaseReplySuccess; |
37 |
|
|
} |
38 |
✓✓ |
2 |
} else if (status == "path_busy") { |
39 |
|
|
const JSON *time_remaining = JsonDocument::SearchInObject( |
40 |
|
1 |
reply->root(), "time_remaining", JSON_INT); |
41 |
✓✗ |
1 |
if (time_remaining != NULL) { |
42 |
|
|
LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %d", |
43 |
|
1 |
time_remaining->int_value); |
44 |
|
1 |
return kLeaseReplyBusy; |
45 |
|
|
} |
46 |
✓✗ |
1 |
} else if (status == "error") { |
47 |
|
|
const JSON *reason = |
48 |
|
1 |
JsonDocument::SearchInObject(reply->root(), "reason", JSON_STRING); |
49 |
✓✗ |
1 |
if (reason != NULL) { |
50 |
|
1 |
LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value); |
51 |
|
|
} |
52 |
|
|
} else { |
53 |
|
|
LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", |
54 |
|
|
status.c_str()); |
55 |
|
|
} |
56 |
|
|
} |
57 |
|
|
|
58 |
|
1 |
return kLeaseReplyFailure; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
5 |
LeaseReply ParseDropReply(const CurlBuffer &buffer) { |
62 |
✓✓ |
5 |
if (buffer.data.size() == 0) { |
63 |
|
1 |
return kLeaseReplyFailure; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
4 |
const UniquePtr<const JsonDocument> reply(JsonDocument::Create(buffer.data)); |
67 |
✓✓✗✓ ✓✓ |
4 |
if (!reply || !reply->IsValid()) { |
68 |
|
1 |
return kLeaseReplyFailure; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
const JSON *result = |
72 |
|
3 |
JsonDocument::SearchInObject(reply->root(), "status", JSON_STRING); |
73 |
✓✗ |
3 |
if (result != NULL) { |
74 |
|
3 |
const std::string status = result->string_value; |
75 |
✓✓ |
3 |
if (status == "ok") { |
76 |
|
1 |
LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
77 |
|
1 |
return kLeaseReplySuccess; |
78 |
✓✓ |
2 |
} else if (status == "invalid_token") { |
79 |
|
1 |
LogCvmfs(kLogCvmfs, kLogStdout, "Error: invalid session token"); |
80 |
✓✗ |
1 |
} else if (status == "error") { |
81 |
|
|
const JSON *reason = |
82 |
|
1 |
JsonDocument::SearchInObject(reply->root(), "reason", JSON_STRING); |
83 |
✓✗ |
1 |
if (reason != NULL) { |
84 |
|
1 |
LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value); |
85 |
|
|
} |
86 |
|
|
} else { |
87 |
|
|
LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", |
88 |
|
|
status.c_str()); |
89 |
|
|
} |
90 |
|
|
} |
91 |
|
|
|
92 |
|
2 |
return kLeaseReplyFailure; |
93 |
|
|
} |