⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/EnhPi3/Http.cpp,v $
 * $Date: 2004/07/04 19:32:22 $
 *
 Description:

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <assert.h>
#include <stdio.h>

#include "PICompat.h"
#include "PIString.h"

#include "resrc1.h"
#include "Common.h"

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
static HWND hWndSrvStamp = 0;
static HWND hWndList = 0;
static HWND hWndGet = 0;
static HWND hWndHead = 0;
static HWND hWndPost = 0;
static HWND hWndPut = 0;
static HWND hWndDelete = 0;
static HWND hWndOptions = 0;
static HWND hWndTrace = 0;
static HWND hWndDescription = 0;
static HWND hWndAnnotation = 0;
static HWND hWndUploadPath = 0;
static HWND hWndUploadLimit = 0;
static HWND hWndChunkLimit = 0;
static HWND hWndRanges = 0;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void Internal_loadItems( HWND hDlg )
{
	/*
	** Clear any existing items out of list
	*/
	::SendMessage( hWndList, LB_RESETCONTENT, 0, 0 );

	/*
	** Load all index files into list box
	*/
	int iTmp = 0;
	for(;; iTmp++ )
		{
		const char *pVal = AD_lookupValueEx( __pConfig, "HTTP", "IndexFile", iTmp );
		if ( !pVal ) { break; };
		::SendMessage( hWndList, LB_ADDSTRING, 0, (LPARAM)pVal );
		}

	::EnableWindow( ::GetDlgItem( hDlg, IDC_REMOVE ),
		::SendMessage( hWndList, LB_GETCOUNT, 0, 0 ));
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static HWND __hWndDlgOK = 0;
static HWND __hWndDlgIndexFile = 0;
static BOOL CALLBACK NewIdxFileDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
	LPARAM lParam)
{
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	switch (uMsg)
		{
		case WM_INITDIALOG:
			__hWndDlgOK = GetDlgItem( hDlg, IDOK );
			__hWndDlgIndexFile = GetDlgItem( hDlg, IDC_INDEX1 );
			assert( __hWndDlgOK && __hWndDlgIndexFile );
			SendMessage( __hWndDlgIndexFile, EM_SETLIMITTEXT, 256, 0 );
			EnableWindow( __hWndDlgOK, FALSE );
			break;

		case WM_COMMAND:
			switch (GET_WM_COMMAND_ID(wParam, lParam))
				{
				case IDOK:
					{
					/*
					** Validate content of edit field
					*/
					*szBuf = '\0';
					SendMessage( __hWndDlgIndexFile, WM_GETTEXT, BUF_SIZE, 
						(LPARAM)szBuf );
					PIString sIdxFile( szBuf );
					int i;
					int iCount = SendMessage( hWndList, CB_GETCOUNT, 0, 0 );
					for( i=0; i<iCount; i++ )
						{
						*szBuf = '\0';
						SendMessage( hWndList, LB_GETTEXT, i, (LPARAM)szBuf );
						if ( sIdxFile == szBuf )
							{
							sprintf( szBuf, "IndexFile '%s' already exists.",
								(const char *)sIdxFile );
							MessageBox( hDlg, szBuf, "New Index File",
								MB_OK | MB_ICONEXCLAMATION );
							return (TRUE);
							};
						};

					::SendMessage( hWndList, LB_ADDSTRING, 0, (LPARAM)szBuf );
					};
					EndDialog( hDlg, 1 );
					break;

				case IDCANCEL:
					EndDialog( hDlg, 0 );
					break;

				case IDC_INDEX1:
					if ( GET_WM_COMMAND_CMD(wParam, lParam)==EN_CHANGE )
						{
						if ( SendMessage( GET_WM_COMMAND_HWND(wParam, lParam ), EM_LINELENGTH,
							0, 0 )>0 )
							{ EnableWindow( __hWndDlgOK, TRUE ); }
						else
							{ EnableWindow( __hWndDlgOK, FALSE ); }
						}
					else
						return (FALSE);
					break;

				default:;
				};

		default:
			return (FALSE);
		};

	return (TRUE);
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static BOOL CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
	LPARAM lParam)
{
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	switch (uMsg)
		{
		case WM_INITDIALOG:
			{
			const char *pA = AD_lookupValue( __pConfig, "General", "Admin" );
			hWndSrvStamp = GetDlgItem( hDlg, IDC_SERVERSTAMP );
			hWndList = GetDlgItem( hDlg, IDC_LIST );
			hWndGet = GetDlgItem( hDlg, IDC_GET );
			hWndHead = GetDlgItem( hDlg, IDC_HEAD );
			hWndPost = GetDlgItem( hDlg, IDC_POST );
			hWndPut = GetDlgItem( hDlg, IDC_PUT );
			hWndDelete = GetDlgItem( hDlg, IDC_DELETE );
			hWndOptions = GetDlgItem( hDlg, IDC_OPTIONS );
			hWndTrace = GetDlgItem( hDlg, IDC_TRACE );
			hWndDescription = GetDlgItem( hDlg, IDC_DESCR );
			hWndAnnotation = GetDlgItem( hDlg, IDC_ANNOTATION );
			hWndUploadPath = GetDlgItem( hDlg, IDC_DOCUMENTROOT );
			hWndUploadLimit = GetDlgItem( hDlg, IDC_LIMIT );
			hWndChunkLimit = GetDlgItem( hDlg, IDC_CHUNKED );
			hWndRanges = GetDlgItem( hDlg, IDC_RANGES );

			assert( hWndSrvStamp && hWndList );
			assert( hWndGet && hWndHead && hWndPost && hWndPut );
			assert( hWndDelete && hWndOptions && hWndTrace );
			assert( hWndDescription && hWndAnnotation );
			assert( hWndUploadPath && hWndUploadLimit );
			assert( hWndChunkLimit && hWndRanges );
			assert( pA );

			/* --- just for sanity --- */
			::SendMessage( hWndSrvStamp, EM_SETLIMITTEXT, 256, 0 );
			::SendMessage( hWndUploadPath, EM_SETLIMITTEXT, 256, 0 );
			::SendMessage( hWndUploadLimit, EM_SETLIMITTEXT, 256, 0 );

			/* --- General --- */
			const char *pVal = AD_lookupValue( __pConfig, "HTTP", "ServerStamp" );
			::SendMessage( hWndSrvStamp, WM_SETTEXT, 0, (LPARAM)pVal );
			Internal_loadItems( hDlg );

			/* --- General Options --- */
			pVal = AD_lookupValue( __pConfig, "HTTP", "ChunkLimit" );
			::SendMessage( hWndChunkLimit, BM_SETCHECK, (WPARAM)(pVal[0]), 0 );
			pVal = AD_lookupValue( __pConfig, "HTTP", "Ranges" );
			::SendMessage( hWndRanges, BM_SETCHECK, (WPARAM)(pVal[0]), 0 );

			/* --- Methods --- */
			int iTmp = atoi(AD_lookupValue( __pConfig, "HTTP", "Methods" ));
			::SendMessage( hWndGet, BM_SETCHECK, (WPARAM)(iTmp & 1), 0 );
			::SendMessage( hWndHead, BM_SETCHECK, (WPARAM)(iTmp & 2), 0 );
			::SendMessage( hWndPost, BM_SETCHECK, (WPARAM)(iTmp & 4), 0 );
			::SendMessage( hWndPut, BM_SETCHECK, (WPARAM)(iTmp & 8), 0 );
			::SendMessage( hWndDelete, BM_SETCHECK, (WPARAM)(iTmp & 16), 0 );
			::SendMessage( hWndOptions, BM_SETCHECK, (WPARAM)(iTmp & 32), 0 );
			::SendMessage( hWndTrace, BM_SETCHECK, (WPARAM)(iTmp & 64), 0 );

			/* --- Upload Options --- */
			pVal = AD_lookupValue( __pConfig, "HTTP", "Description" );
			::SendMessage( hWndDescription, BM_SETCHECK, (WPARAM)(pVal[0]), 0 );
			pVal = AD_lookupValue( __pConfig, "HTTP", "Annotation" );
			::SendMessage( hWndAnnotation, BM_SETCHECK, (WPARAM)(pVal[0]), 0 );
			pVal = AD_lookupValue( __pConfig, "HTTP", "UploadPath" );
			::SendMessage( hWndUploadPath, WM_SETTEXT, 0, (LPARAM)pVal );
			pVal = AD_lookupValue( __pConfig, "HTTP", "UploadLimit" );
			::SendMessage( hWndUploadLimit, WM_SETTEXT, 0, (LPARAM)pVal );
			
			iTmp = ::SendMessage( hWndPost, BM_GETCHECK, 0, 0 );
			::EnableWindow( hWndDescription, iTmp );
			::EnableWindow( hWndAnnotation, iTmp );
			::EnableWindow( hWndUploadPath, iTmp );
			::EnableWindow( hWndUploadLimit, iTmp );

			AD_UnChanged( hDlg );
			};
			break;

		case WM_COMMAND:
			if ( GET_WM_COMMAND_CMD(wParam, lParam)==BN_CLICKED )
				{
				switch( GET_WM_COMMAND_ID( wParam, lParam) )
					{
					case IDC_POST:
						::EnableWindow( hWndDescription,
							::SendMessage( hWndPost, BM_GETCHECK, 0, 0 ));
						::EnableWindow( hWndAnnotation,
							::SendMessage( hWndPost, BM_GETCHECK, 0, 0 ));
						::EnableWindow( hWndUploadPath,
							::SendMessage( hWndPost, BM_GETCHECK, 0, 0 ));
						::EnableWindow( hWndUploadLimit,
							::SendMessage( hWndPost, BM_GETCHECK, 0, 0 ));
						AD_Changed( hDlg );
						break;

					case IDC_ADD:
						if ( DialogBox(
							NULL,
							MAKEINTRESOURCE( IDD_NEWINDEXFILE ),
							hDlg,
							(DLGPROC)NewIdxFileDialogProc
							) )
							{
							::EnableWindow( ::GetDlgItem( hDlg, IDC_REMOVE ),
								::SendMessage( hWndList, LB_GETCOUNT, 0, 0 ));
							AD_Changed( hDlg );
							};
						break;

					case IDC_REMOVE:
						{
						int iSel = ::SendMessage( hWndList, LB_GETCURSEL, 0, 0 );
						if ( iSel==CB_ERR ) { break; };

						::SendMessage( hWndList, LB_DELETESTRING, iSel, 0 );
						int iCount = SendMessage( hWndList, LB_GETCOUNT, 0, 0);
						if (iSel >= iCount) {
							SendMessage( hWndList, LB_SETCURSEL, iCount-1, 0 );
						} else {
							SendMessage( hWndList, LB_SETCURSEL, iSel, 0 );
						}

						::EnableWindow( ::GetDlgItem( hDlg, IDC_REMOVE ),
							::SendMessage( hWndList, LB_GETCOUNT, 0, 0 ));
						AD_Changed( hDlg );
						}
						break;

					default:;
					}
				}
			if ( GET_WM_COMMAND_CMD(wParam, lParam)==EN_CHANGE )
				/* --- mark property sheet as modified --- */
				{ AD_Changed( hDlg ); }
			if ( GET_WM_COMMAND_CMD(wParam, lParam)==BN_CLICKED )
				{
				/* --- mark property sheet as modified --- */
				AD_Changed( hDlg );
				}
			break;

		case WM_NOTIFY:
			switch( ((NMHDR *)lParam)->code )
				{
				case PSN_QUERYCANCEL:
					return AD_cancel( hDlg );
				
				case PSN_KILLACTIVE:
					if ( AD_IsChanged() )
					{
					/* --- General --- */
					*szBuf = '\0';
					::SendMessage( hWndSrvStamp, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "HTTP", "ServerStamp", szBuf ); 

					AD_deleteValue( __pConfig, "HTTP", "IndexFile" ); 
					int iCount = SendMessage( hWndList, LB_GETCOUNT, 0, 0);
					int iTmp = 0;
					for (;iTmp<iCount;iTmp++)
						{
						*szBuf = '\0';
						SendMessage( hWndList, LB_GETTEXT, iTmp, (LPARAM)szBuf );
						AD_addValue( __pConfig, "HTTP", "IndexFile", szBuf ); 
						}

					/* --- General Options --- */
					if ( ::SendMessage( hWndChunkLimit, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{ AD_replaceValue( __pConfig, "HTTP", "ChunkLimit", "8192" ); }
					else
						{ AD_replaceValue( __pConfig, "HTTP", "ChunkLimit", "" ); };

					if ( ::SendMessage( hWndRanges, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{ AD_replaceValue( __pConfig, "HTTP", "Ranges", "bytes" ); }
					else
						{ AD_replaceValue( __pConfig, "HTTP", "Ranges", "" ); };

					/* --- Methods --- */
					iTmp = 0;
					if ( ::SendMessage( hWndGet, BM_GETCHECK, 0, 0 ))
						{ iTmp += 1; };
					if ( ::SendMessage( hWndHead, BM_GETCHECK, 0, 0 ))
						{ iTmp += 2; };
					if ( ::SendMessage( hWndPost, BM_GETCHECK, 0, 0 ))
						{ iTmp += 4; };
					if ( ::SendMessage( hWndPut, BM_GETCHECK, 0, 0 ))
						{ iTmp += 8; };
					if ( ::SendMessage( hWndDelete, BM_GETCHECK, 0, 0 ))
						{ iTmp += 16; };
					if ( ::SendMessage( hWndOptions, BM_GETCHECK, 0, 0 ))
						{ iTmp += 32; };
					if ( ::SendMessage( hWndTrace, BM_GETCHECK, 0, 0 ))
						{ iTmp += 64; };
					AD_replaceValue( __pConfig, "HTTP", "Methods",
						itoa(iTmp, szBuf, 10));

					/* --- Upload Options --- */
					if ( ::SendMessage( hWndDescription, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{ AD_replaceValue( __pConfig, "HTTP", "Description", "On" ); }
					else
						{ AD_replaceValue( __pConfig, "HTTP", "Description", "" ); };

					if ( ::SendMessage( hWndAnnotation, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{ AD_replaceValue( __pConfig, "HTTP", "Annotation", "On" ); }
					else
						{ AD_replaceValue( __pConfig, "HTTP", "Annotation", "" ); };

					*szBuf = '\0';
					::SendMessage( hWndUploadPath, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "HTTP", "UploadPath", szBuf ); 

					*szBuf = '\0';
					::SendMessage( hWndUploadLimit, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "HTTP", "UploadLimit", szBuf ); 
					}
					SetWindowLong( hDlg, DWL_MSGRESULT, FALSE );
					return (TRUE);

				default:
					return (FALSE);
				};
			break;

		default:
			return (FALSE);
		};

	return (TRUE);
}

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
DLGPROC fnHttp = (DLGPROC)DialogProc;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -