Skip to content

Commit

Permalink
Merge pull request xapi-project#5302 from kc284/master
Browse files Browse the repository at this point in the history
Allow passing extra headers into the HTTP calls (CP-33676) +  markdown fixes
  • Loading branch information
kc284 authored Jan 12, 2024
2 parents d76169a + 0c00825 commit 6121125
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
6 changes: 3 additions & 3 deletions ocaml/sdk-gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ XenAPI's datamodel. The generation code is written in OCaml and is contained in
this directory.

The Python module is not auto-generated, it can be found at
[XenAPI.py][../../scripts/examples/python/XenAPI/XenAPI.py].
[XenAPI.py](../../scripts/examples/python/XenAPI/XenAPI.py).

To compile the generated source code, follow the instructions in the corresponding
README files. The (patched) third party libraries required for the compilation
Expand All @@ -23,7 +23,7 @@ you get started with the SDK.
## Licensing

The SDK generation code is licensed under the GNU LGPLv2 license. Please see this
[LICENSE][../../LICENSE] file for more information.
[LICENSE](../../LICENSE) file for more information.

The generated code and the examples are licensed under the BSD 2-Clause license.
Please see this [LICENSE][LICENSE] file for more information.
Please see this [LICENSE](LICENSE) file for more information.
65 changes: 47 additions & 18 deletions ocaml/sdk-gen/csharp/autogen/src/HTTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,9 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
}


private static Stream DoHttp(Uri uri, IWebProxy proxy, bool nodelay, int timeout_ms, params string[] headers)
private static Stream DoHttp(Uri uri, IWebProxy proxy, bool noDelay, int timeoutMs, params string[] headers)
{
Stream stream = ConnectStream(uri, proxy, nodelay, timeout_ms);
Stream stream = ConnectStream(uri, proxy, noDelay, timeoutMs);

int redirects = 0;

Expand All @@ -687,43 +687,72 @@ private static Stream DoHttp(Uri uri, IWebProxy proxy, bool nodelay, int timeout

stream.Flush();
}
while (ReadHttpHeaders(ref stream, proxy, nodelay, timeout_ms));
while (ReadHttpHeaders(ref stream, proxy, noDelay, timeoutMs));

return stream;
}

/// <summary>
/// Adds HTTP CONNECT headers returning the stream ready for use
/// </summary>
public static Stream HttpConnectStream(Uri uri, IWebProxy proxy, String session, int timeoutMs)
public static Stream HttpConnectStream(Uri uri, IWebProxy proxy, string session, int timeoutMs, Dictionary<string, string> additionalHeaders = null)
{
return DoHttp(uri, proxy, true, timeoutMs,
string.Format("CONNECT {0} HTTP/1.0", uri.PathAndQuery),
string.Format("Host: {0}", uri.Host),
string.Format("Cookie: session_id={0}", session));
var allHeaders = new List<string>
{
$"CONNECT {uri.PathAndQuery} HTTP/1.0",
$"Host: {uri.Host}",
$"Cookie: session_id={session}"
};

if (additionalHeaders != null)
{
foreach (var kvp in additionalHeaders)
allHeaders.Add($"{kvp.Key}: {kvp.Value}");
}

return DoHttp(uri, proxy, true, timeoutMs, allHeaders.ToArray());
}

/// <summary>
/// Adds HTTP PUT headers returning the stream ready for use
/// </summary>
public static Stream HttpPutStream(Uri uri, IWebProxy proxy, long contentLength, int timeoutMs)
public static Stream HttpPutStream(Uri uri, IWebProxy proxy, long contentLength, int timeoutMs, Dictionary<string, string> additionalHeaders = null)
{
return DoHttp(uri, proxy, false, timeoutMs,
string.Format("PUT {0} HTTP/1.0", uri.PathAndQuery),
string.Format("Host: {0}", uri.Host),
string.Format("Content-Length: {0}", contentLength));
var allHeaders = new List<string>
{
$"PUT {uri.PathAndQuery} HTTP/1.0",
$"Host: {uri.Host}",
$"Content-Length: {contentLength}"
};

if (additionalHeaders != null)
{
foreach (var kvp in additionalHeaders)
allHeaders.Add($"{kvp.Key}: {kvp.Value}");
}

return DoHttp(uri, proxy, false, timeoutMs, allHeaders.ToArray());
}

/// <summary>
/// Adds HTTP GET headers returning the stream ready for use
/// </summary>
public static Stream HttpGetStream(Uri uri, IWebProxy proxy, int timeoutMs)
public static Stream HttpGetStream(Uri uri, IWebProxy proxy, int timeoutMs, Dictionary<string, string> additionalHeaders = null)
{
return DoHttp(uri, proxy, false, timeoutMs,
string.Format("GET {0} HTTP/1.0", uri.PathAndQuery),
string.Format("Host: {0}", uri.Host));
}
var allHeaders = new List<string>
{
$"GET {uri.PathAndQuery} HTTP/1.0",
$"Host: {uri.Host}"
};

if (additionalHeaders != null)
{
foreach (var kvp in additionalHeaders)
allHeaders.Add($"{kvp.Key}: {kvp.Value}");
}

return DoHttp(uri, proxy, false, timeoutMs, allHeaders.ToArray());
}

/// <summary>
/// A general HTTP PUT method, with delegates for progress and cancelling. May throw various exceptions.
Expand Down

0 comments on commit 6121125

Please sign in to comment.