ホーム » 検索結果: 簡易 Web
検索結果: 簡易 Web
簡易 Web サーバ C++/CLI
先日の C# のコードから C++/CLI に書き直したものの 更新版 .
index.html の作成と,ContentType の設定,日本語ファイル名への対応など.
#ifdef __cplusplus_cli
#using <System.dll>
#using <System.Web.dll>
#include <vcclr.h>
#endif
///////////////////////////////////////////////////////////////////////////
#include "S_Exec.hxx"
#include "str_CLI.hxx"
#include "filestat.hxx"
#include "filepath.hxx"
#include "ask_path.hxx"
#include "itls_tmp.hxx"
#include "textfile.hxx"
#include "htmout.hxx"
#include "stringfn.hxx"
///////////////////////////////////////////////////////////////////////////
struct MIME_type {
LPCTSTR FExt ;
LPCTSTR Type ;
} ;
// https://developer.mozilla.org/ja/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
const MIME_type MIME_type_tbl[] =
{
_T("htm" ) , _T("text/html") ,
_T("html") , _T("text/html") ,
_T("txt" ) , _T("text/plain") ,
_T("bmp" ) , _T("image/bmp") ,
_T("jpeg") , _T("image/jpeg") ,
_T("jpg" ) , _T("image/jpeg") ,
_T("png" ) , _T("image/png") ,
_T("svg" ) , _T("image/svg+xml") ,
_T("bin" ) , _T("application/octet-stream") ,
_T("pdf" ) , _T("application/pdf") ,
_T("zip" ) , _T("application/zip") ,
_T(" " ) , _T("") ,
_T("\0" ) , _T("") ,
_T("" ) , _T("") ,
} ;
inline tstring get_MIME_type (LPCTSTR ext_)
{
tstring type = _T("application/octet-stream") ;
tstring ext = ::String_ToLower(ext_) ;
if (ext.empty()) {
return type ;
}
const MIME_type* mm_ty = MIME_type_tbl ;
{
for (size_t index=0 ; index<countof(MIME_type_tbl) ; index++)
{
tstring fext = mm_ty[index].FExt ;
tstring ftyp = mm_ty[index].Type ;
if (fext.length() == 0) { continue ; }
if (fext.length() != ext.length()) { continue ; }
if (ext == fext) {
return mm_ty[index].Type ;
}
}
}
return type ;
}
///////////////////////////////////////////////////////////////////////////
inline tstring HT_Make_index_content (c_tstring& fold)
{
v_tstring sub_folds = ::EnumFolders(fold.c_str()) ;
v_tstring htm_files = ::EnumFiles (fold.c_str(),_T("*.htm*")) ;
tstring foldName = ::Path_GetTitle(fold) ;
tstring htm_str ;
{
Xml_E htm = HtmOut::html() ;
{
Xml_E head(HTM_head) ;
{
head.AddChild(HtmOut::charset_UTF_8()) ;
head.AddChild(HtmOut::meta_viewport()) ;
head.AddChild(HtmOut::title(foldName)) ;
}
{
head.AddChild(HtmOut::comment()) ;
}
htm.AddChild(head) ;
}
{
Xml_E body(HTM_body) ;
{
{
body.AddChild(HtmOut::a_parent()) ;
body.AddChild(HtmOut::hr()) ;
}
{
for (size_t index=0 ; index<sub_folds.size() ; index++) {
tstring fold = sub_folds[index] ;
fold = ::Path_DelLastSP(fold) ;
Xml_E a_fold = HtmOut::a(::Path_GetName(fold)+_T("/")) ;
body.AddChild(a_fold) ;
body.AddChild(HtmOut::br()) ;
}
body.AddChild(HtmOut::hr()) ;
}
{
for (size_t index=0 ; index<htm_files.size() ; index++) {
tstring html = htm_files[index] ;
Xml_E a_html = HtmOut::a(::Path_GetName(html)) ;
body.AddChild(a_html) ;
body.AddChild(HtmOut::br()) ;
}
}
}
htm.AddChild(body) ;
}
htm_str = htm.ToText() ;
}
return htm_str ;
}
tstring Make_index (c_tstring& fold)
{
tstring result = ::HT_Make_index_content(fold) ;
return result ;
}
///////////////////////////////////////////////////////////////////////////
bool web_server (c_tstring& fold,const u_16 port)
{
tstring root_ = fold ;
tstring port_ = ::To_tstring(port) ;
tstring pref_ = _T("http://127.0.0.1:")+port_+_T("/") ;
System::String^ root = ::to_gcString(root_) ;
System::String^ prefix = ::to_gcString(pref_) ;
System::Console::WriteLine(prefix) ;
System::Net::HttpListener^ listener = gcnew System::Net::HttpListener();
listener->Prefixes->Add(prefix);
listener->Start();
while (true) {
System::Net::HttpListenerContext^ context = listener->GetContext();
System::Net::HttpListenerRequest^ req = context->Request;
System::Net::HttpListenerResponse^ res = context->Response;
System::String^ path = root + req->RawUrl->Replace("/", "\\");
path = System::Web::HttpUtility::UrlDecode(path) ;
{
System::Console::WriteLine(req->RawUrl);
System::Console::WriteLine(path) ;
}
if (System::IO::File::Exists(path)) {
}
if (System::IO::File::Exists(path)) {
tstring ext = ::Path_GetExtLow(::to_tstring(path)) ;
array<System::Byte>^ content = System::IO::File::ReadAllBytes(path);
res->ContentType = ::to_gcString(::get_MIME_type(ext.c_str())) ;
res->OutputStream->Write(content, 0, content->Length);
}
else {
tstring cnt_index = ::Make_index (::to_tstring(path)) ;
array<System::Byte>^ content ;
{
static long i_count = 0 ;
i_count++ ;
tstring tmp_path = ::Get_i_Tools_tmp_date() ;
tstring htm_name = ::To_tstring(port) + _T("_") + ::u32to0t(i_count,10,4) + _T(".htm") ;
tstring out_path = ::Path_AddLastSP(tmp_path) + htm_name ;
::SaveUTF8(out_path.c_str(),cnt_index) ;
content = System::IO::File::ReadAllBytes(::to_gcString(out_path.c_str())) ;
}
res->ContentType = ::to_gcString(::get_MIME_type(_T("htm"))) ;
res->OutputStream->Write(content, 0, content->Length);
}
res->Close();
}
return true ;
}
///////////////////////////////////////////////////////////////////////////
bool test (c_tstring& str)
{
tstring fold = str ;
{
if (::File_IsDirectory(fold)) { ; }
else { fold = ::Path_GetDir(fold) ; }
}
std::terr << fold << std::endl ;
{
u_16 tick = u_16(::GetTickCount()) ;
u_16 port = u_16(50000 + (tick&0x1fff)) ;
{
tstring port_ = ::To_tstring(port) ;
tstring pref_ = _T("http://127.0.0.1:")+port_+_T("/") ;
S_Exec se ;
se.SetFile(pref_.c_str()) ;
se.Execute() ;
}
::web_server(fold,port) ;
}
return true ;
}
///////////////////////////////////////////////////////////////////////////
int _tmain (int argc,_TCHAR* argv[])
{
tstring path ;
{
#ifdef OFN_filter_All
path = ::ask_path(false) ;
// path = ::ask_path(true) ;
#else
path = ::ask_cli(_T("folder ... ? =")) ;
#endif
}
if (!path.empty()) {
::test(path) ;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
#include "messbar.cxx"
* 幾つかのコードが揃っていないため,そのままではビルドできません.
https://jml.mish.work/index.php/i-tools/web-svr.html
2024/11/02
VC 2005 でビルド可能な一式を用意しました.
https://itl.mish.work/i_Tools/Doc/blog/vc/T_h_lstn.zip
簡易 Web サーバ C#
System.Net.HttpListener.dll で比較的簡単にできるみたいなので,ちょっと調べてみた.
最初に見つけたのは次の所.
.NET TIPS 簡易Webサーバを実装するには?[2.0のみ、C#、VB]
Win10 C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe でコンパイルして問題なく動作する.
そのままのコードでは,管理者として起動しなければならないのと,ルートが固定のため少し変更.
string root = @"c:\wwwroot\"; // ドキュメント・ルート
root = @".\";
string prefix = "http://*/"; // 受け付けるURL
prefix = "http://localhost:65432/" ;
これで exe を対象のルートにコピーして,起動させることができる.
localhost ではなく 127.0.0.1 を指定すると //localhost:65432/… としてもアクセス可能.
prefix = "http://127.0.0.1:65432/" ;
参考にさせてもらった所.
https://yryr.me/programming/local-http-server.html
https://www.3ace-net.co.jp/blog/201304151845.html
https://d01tsumath.hatenablog.com/entry/2019/08/16/190000
これらをテストしていて,WebGL のページを表示すると,極端に遅くなる.
以前は FHD 環境だったが,先日から 4K でいろいろと…
そのため WebGL などのページを表示すると,GPU のメモリを大量に使用するみたい.
タスクマネージャの「専用 GPU メモリ」は 10 GB 程度になっているが,どこを使用しているの?
設定にもよるかもしれないが Firefox で次の所を表示すると「専用 GPU メモリ」の使用量が増えていく.
https://threejs.org/examples/#webgl_instancing_performance
C# のコードを C++/CLI に
簡易 Web サーバ C++/CLI
https://jml.mish.work/index.php/i-tools/web-svr.html
簡易 Web サーバ
次の様なものをブラウザで表示するために…
http://mish.html.xdomain.jp/Test/WebGL/Sphere.html
「Web Server for Chrome」は以前書いた.
http://test_wp.mish.work/2022/01/chrome-web.html
Win11 環境で同様に実行すると,次の様な表示が…
開発環境には VS 2022 などが入っているので Python を使用した方法
…\Test\Web\WebGL\ に html などをコピー.
コマンドプロンプトで …\Test に移動.
py -m http.server
これで 127.0.0.1:8000 でアクセスできる.
終了は「Ctrl」+「C」.
ASUSTOR NAS だと,
python -m http.server
QNAP NAS や Synology NAS でも同様.Raspberry Pi にもあった.
検索すると System.Net.HttpListener.dll と言うのが見つかる.
それを使っていると思われる次の所からコードを拝借.
Windows 標準で Web サーバを起動する
勉強を兼ねて,ちょっとやってみるか.
2023/01/17
https://jml.mish.work/index.php/i-tools/web-svr.html
WebGL での 3D データ表示
さらに,以前作成した Web サーバ を利用して WebGL での表示に.
bool test (void)
{
Vd2A pts ;
{
pts.push_back(Vd2( 0, 0)) ;
pts.push_back(Vd2( 70, 0)) ;
pts.push_back(Vd2( 90, 50)) ;
pts.push_back(Vd2(100,100)) ;
pts.push_back(Vd2( 50,100)) ;
pts.push_back(Vd2( 30, 70)) ;
pts.push_back(Vd2( 40, 30)) ;
pts.push_back(Vd2( 0, 0)) ;
}
vv_PLF vvplf ;
{
Vd3A v3a = ::ToVd3A(pts) ;
Vd4A v4a = ::ToVd4A(v3a) ;
PLF plf_l(PLF::line,v4a) ;
PLF plf_f(PLF::face,v4a) ;
v_PLF v_plf ;
v_plf.push_back(plf_l) ;
v_plf.push_back(plf_f) ;
vvplf.push_back(v_plf) ;
}
{
tstring tmp_path = ::Get_i_Tools_tmp_date() ;
tstring out_name = ::Path_AddLastSP(tmp_path) + ::Now_Format(_T("%H%M%S")) + _T("__.htm") ;
tstring outtname = ::Path_AddLastSP(tmp_path) + ::Now_Format(_T("%H%M%S")) + _T("_t.htm") ;
tstring outnname = ::Path_AddLastSP(tmp_path) + ::Now_Format(_T("%H%M%S")) + _T("_n.htm") ;
{
GonsA gnsa = ::PLF_ToGonsA(vvplf) ;
::GonsA_ToWGL(gnsa,out_name.c_str()) ;
gnsa = ::GonsA_Triangulation(gnsa) ;
::GonsA_ToWGL(gnsa,outtname.c_str()) ;
gnsa = ::GonsA_CalcNormal (gnsa) ;
::GonsA_ToWGL(gnsa,outnname.c_str()) ;
}
{
::start_web_server(tmp_path) ;
}
}
tstring g3_d_exe = ::get_g3_d_exe() ;
if (!g3_d_exe.empty()) {
tstring tmp_path = ::Get_i_Tools_tmp_date() ;
tstring ipl_name = ::Path_AddLastSP(tmp_path) + ::Now_Format(_T("%H%M%S")) + _T(".ipl") ;
::To_ipl (vvplf,ipl_name.c_str()) ;
::start_g3_d(ipl_name.c_str()) ;
}
return true ;
}
C# のコードを C++/CLI に
先日の「簡易 Web サーバ C#」のコードを C++ で.
///////////////////////////////////////////////////////////////////////////////////
// C# -> C++/CLI
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <tchar.h>
//using System;
//using System.IO;
//using System.Net;
#using <System.dll>
bool test (void)
{
//
// class SimpleWebServer
// {
// static void Main()
// {
// string root = @"c:\wwwroot\"; // ドキュメント・ルート
// root = @".\";
System::String^ root = "./" ;
// string prefix = "http://*/"; // 受け付けるURL
// prefix = "http://127.0.0.1:65432/" ;
System::String^ prefix = "http://127.0.0.1:55555/" ;
//
// HttpListener listener = new HttpListener();
System::Net::HttpListener^ listener = gcnew System::Net::HttpListener();
// listener. Prefixes. Add(prefix); // プレフィックスの登録
listener->Prefixes->Add(prefix);
// listener. Start();
listener->Start();
//
// while (true) {
while (true) {
// HttpListenerContext context = listener. GetContext();
System::Net::HttpListenerContext^ context = listener->GetContext();
// HttpListenerRequest req = context. Request;
System::Net::HttpListenerRequest^ req = context->Request;
// HttpListenerResponse res = context. Response;
System::Net::HttpListenerResponse^ res = context->Response;
//
// Console. WriteLine(req. RawUrl);
System::Console::WriteLine(req->RawUrl);
//
// // リクエストされたURLからファイルのパスを求める
// string path = root + req. RawUrl. Replace("/", "\\");
System::String^ path = root + req->RawUrl->Replace("/", "\\");
//
// // ファイルが存在すればレスポンス・ストリームに書き出す
// if ( File. Exists(path)) {
if (System::IO::File::Exists(path)) {
// byte[] content = File. ReadAllBytes(path);
array<System::Byte>^ content = System::IO::File::ReadAllBytes(path);
// res. OutputStream. Write(content, 0, content. Length);
res->OutputStream->Write(content, 0, content->Length);
// }
}
// res. Close();
res->Close();
// }
}
// }
// }
//
return true ;
}
int _tmain(int argc, _TCHAR* argv[])
{
::test() ;
return 0;
}
NAS 上の「ディスク上のサイズ」
比較的小さなファイル(数 K バイト)をバックアップしていて,サイズを見ると異常に大きい?
292 個のファイルで「ディスク上のサイズ」が 292 MB の表示.
ファイルを置いているのは,NAS の HDL-AHW .
Win7 で利用していた時は気にならなかったと思い,そちらで見ると問題ない.
今度は 1 バイトのファイルを作成して同じ様にみると,Win10 では 1 MB となる.
異なるメーカーの NAS の情報だが,その様になってしまうみたい.
BUFFALO リンクステーション スマホ・タブレットで使える ネットワークHDD
LinkStation LS420D0402C
WD Cloud や Synology NAS DS116 では問題ない.
2021/04/05
HVL-LS4 を追加.
新しい機種だと正しく表示されるのかと思ったが,そうではなかった.
Win10 では 1 バイトのファイルが 1 MB の表示になる.
2022/02/15
ASUS ルータの「簡易 NAS 機能」でも同様に 1 MB になっている.