]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/BamHttp_p.h
Stablized HTTP access on all platforms. (issue #54, issue #11)
[bamtools.git] / src / api / internal / io / BamHttp_p.h
1 // ***************************************************************************
2 // BamHttp_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides reading/writing of BAM files on HTTP server
8 // ***************************************************************************
9
10 #ifndef BAMHTTP_P_H
11 #define BAMHTTP_P_H
12
13 //  -------------
14 //  W A R N I N G
15 //  -------------
16 //
17 // This file is not part of the BamTools API.  It exists purely as an
18 // implementation detail. This header file may change from version to version
19 // without notice, or even be removed.
20 //
21 // We mean it.
22
23 #include "api/IBamIODevice.h"
24 #include <string>
25
26 namespace BamTools {
27 namespace Internal {
28
29 class HttpRequestHeader;
30 class HttpResponseHeader;
31 class TcpSocket;
32
33 class BamHttp : public IBamIODevice {
34
35     // ctor & dtor
36     public:
37         BamHttp(const std::string& url);
38         ~BamHttp(void);
39
40     // IBamIODevice implementation
41     public:
42         void Close(void);
43         bool IsOpen(void) const;
44         bool IsRandomAccess(void) const;
45         bool Open(const IBamIODevice::OpenMode mode);
46         int64_t Read(char* data, const unsigned int numBytes);
47         bool Seek(const int64_t& position, const int origin = SEEK_SET);
48         int64_t Tell(void) const;
49         int64_t Write(const char* data, const unsigned int numBytes);
50
51     // internal methods
52     private:
53         void ClearResponse(void);
54         bool ConnectSocket(void);
55         void DisconnectSocket(void);
56         bool EnsureSocketConnection(void);
57         void ParseUrl(const std::string& url);
58         int64_t ReadFromSocket(char* data, const unsigned int numBytes);
59         bool ReceiveResponse(void);
60         bool SendGetRequest(const size_t numBytes = 0x10000);
61         bool SendHeadRequest(void);
62         int64_t WriteToSocket(const char* data, const unsigned int numBytes);
63
64     // data members
65     private:
66
67         // our main socket
68         TcpSocket* m_socket;
69
70         // our connection data
71         std::string m_hostname;
72         std::string m_port;
73         std::string m_filename;
74
75         // our last (active) request & response info
76         HttpRequestHeader*  m_request;
77         HttpResponseHeader* m_response;
78
79         // internal state flags
80         bool m_isUrlParsed;
81
82         // file position
83         int64_t m_filePosition;
84         int64_t m_fileEndPosition;
85         int64_t m_rangeEndPosition;
86 };
87
88 } // namespace Internal
89 } // namespace BamTools
90
91 #endif // BAMHTTP_P_H