comparison horori/searcher/include/net.h @ 0:aaaa401818a1 draft

first commit.
author pyon <pyon@macmini>
date Mon, 24 May 2021 21:32:58 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:aaaa401818a1
1 // Filename : net.h
2 // Last Change: 2020-05-01 ‹à 08:47:39.
3 //
4 #pragma once
5
6 #include <wx/sstream.h>
7 #include <wx/wfstream.h>
8 #include <wx/zstream.h>
9 #include <wx/tarstrm.h>
10 #include <wx/protocol/http.h>
11
12 wxString HttpGetText(wxString addr, wxString port, wxString url)
13 {
14 wxHTTP get;
15 get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
16 while (!get.Connect(addr, wxAtoi(port)))
17 wxSleep(1);
18
19 wxString res;
20 wxInputStream *httpStream = get.GetInputStream(url);
21 if (get.GetError() == wxPROTO_NOERR) {
22 wxStringOutputStream out_stream(&res);
23 httpStream->Read(out_stream);
24 }
25
26 wxDELETE(httpStream);
27 get.Close();
28
29 return res;
30 };
31
32 bool HttpGetFile(wxString addr, wxString port, wxString url, wxString file)
33 {
34 bool ret = false;
35 wxHTTP get;
36 get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
37 while (!get.Connect(addr, wxAtoi(port)))
38 wxSleep(1);
39
40 wxInputStream *httpStream = get.GetInputStream(url);
41 if (get.GetError() == wxPROTO_NOERR) {
42 wxFileOutputStream out_stream(file);
43 httpStream->Read(out_stream);
44 ret = true;
45 }
46
47 wxDELETE(httpStream);
48 get.Close();
49 return ret;
50 };
51
52 bool HttpGetTgzFile(wxString addr, wxString port, wxString url, wxString dir)
53 {
54 bool ret = false;
55 wxHTTP get;
56 get.SetTimeout(30);
57 get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
58 while (!get.Connect(addr, wxAtoi(port)))
59 wxSleep(1);
60
61 wxInputStream *httpStream = get.GetInputStream(url);
62 if (get.GetError() == wxPROTO_NOERR) {
63 //int size = httpStream->GetSize();
64 wxZlibInputStream zlib_istream(httpStream);
65
66 wxTarEntry* entry;
67 wxTarInputStream tar_istream(zlib_istream);
68 int i = 1;
69 while ((entry = tar_istream.GetNextEntry()) != NULL) {
70 //wxString name = entry->GetName();
71 wxFileOutputStream file_ostream(wxString::Format(wxT("%s/%d"), dir, i++));
72 file_ostream.Write(tar_istream);
73 file_ostream.Close();
74 }
75 ret = true;
76 //wxDELETE(httpStream);
77 }
78
79 get.Close();
80 return ret;
81 };
82
83 bool HttpPostFile(wxString addr, wxString port, wxString url, wxString file)
84 {
85 bool ret = false;
86
87 wxMemoryBuffer buf;
88 wxByte b[8192];
89 wxFFile f;
90 if (!f.Open(file, wxT("rb"))) {
91 wxMessageBox(wxT("Cannot open file."));
92 return ret;
93 }
94 for (;;) {
95 size_t len = f.Read(b, sizeof(b));
96 if ((size_t)wxInvalidOffset == len) {
97 return ret;
98 }
99 if (len == 0) break; // success
100 buf.AppendData(b, len);
101 }
102 f.Close();
103
104 wxHTTP post;
105 post.SetTimeout(30);
106 post.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
107 post.SetPostBuffer(wxT("application/gzip"), buf);
108
109 while (!post.Connect(addr, wxAtoi(port)))
110 wxSleep(1);
111
112 wxInputStream *httpStream = post.GetInputStream(url);
113 if (httpStream != NULL) {
114 wxString res;
115 wxStringOutputStream out_stream(&res);
116 httpStream->Read(out_stream);
117 wxDELETE(httpStream);
118 //wxMessageBox(res);
119 ret = true;
120 }
121
122 post.Close();
123 return ret;
124 };
125