ithoran

Boza 2

Dec 26th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. // MR6zad4.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MR6zad4.h"
  6. #include <io.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include <afx.h>
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // The one and only application object
  20.  
  21. CWinApp theApp;
  22.  
  23. using namespace std;
  24.  
  25. void SendFile(CString file)
  26. {
  27.     CFile f;
  28.    
  29.     CFile tmp;
  30.     CString tmps("tmp.log");
  31.     tmp.Open(tmps, CFile::modeCreate | CFile::modeWrite);
  32.     tmp.Write(file, file.GetLength());
  33.  
  34.  
  35.     if (!f.Open(file, CFile::modeRead))
  36.     {
  37.         printf("HTTP/1.1 404 Not Found\nContent-Length: 26\nContent-Type: text/plain\n\n404 Error: File not found.");
  38.         exit(-1);
  39.     }
  40.     DWORD len = f.GetLength();
  41.     printf("Content-Type: image/jpeg\r\n");
  42.     printf("Content-Length: %d\r\n\r\n", len);
  43.  
  44.     int result;
  45.     result = _setmode(_fileno(stdout), _O_BINARY);
  46.     if (result == -1)
  47.     {
  48.         exit(-1);
  49.     }
  50.     else
  51.     {
  52.         for (long i = 0; i < len; i++)
  53.         {
  54.             BYTE buf;
  55.             f.Read(&buf, 1);
  56.             printf("%c", buf);
  57.         }
  58.         fflush(stdout);
  59.         _setmode(_fileno(stdout), _O_TEXT);
  60.         f.Close();
  61.     }
  62. }
  63.  
  64.  
  65. void parse(char* query)
  66. {
  67.     CString cquery(query), file;
  68.     int p=cquery.Find(_T("slika="))+6;
  69.     for (int i = p; i < cquery.GetLength(); i++)
  70.         file=file+cquery[i];
  71.     file="slika1.jpeg";
  72.     SendFile(file);
  73. }
  74.  
  75. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  76. {
  77.     int nRetCode = 0;
  78.  
  79.     // initialize MFC and print and error on failure
  80.     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  81.     {
  82.         // TODO: change error code to suit your needs
  83.         cerr << _T("Fatal Error: MFC initialization failed") << endl;
  84.         nRetCode = 1;
  85.     }
  86.     else
  87.     {
  88.         // TODO: code your application's behavior here.
  89.             char *reqMethod = getenv("REQUEST_METHOD");
  90.             char *queryString = getenv("QUERY_STRING");
  91.             char *POST_INPUT;
  92.             CString s_post("POST");
  93.  
  94.             if (s_post.CompareNoCase(CString(reqMethod)) == 0)
  95.             {
  96.                 if(queryString != NULL)
  97.                     parse(queryString);
  98.                 int br_byte = atoi(getenv("CONTENT_LENGTH"));
  99.                 if (br_byte > 0)
  100.                 {
  101.                     char* postInput = new char[br_byte];
  102.                     scanf("%s", postInput);
  103.                     parse(postInput);
  104.                 }
  105.             }
  106.     }
  107.  
  108.     return nRetCode;
  109. }
Add Comment
Please, Sign In to add comment